Python持久数

时间:2015-04-01 00:17:52

标签: python numbers persistence

需要一些帮助来编写Python中的持久数字程序,以确定具有持久性的最小非负整数 (715 ---> 35 ---> 15 ---> 5)3,然后是4,然后是5,然后是6,然后是7.输出格式如下:

The smallest integer with a persistence of 3 is: 39
The smallest integer with a persistence of 4 is: xx
The smallest integer with a persistence of 5 is: xxx
The smallest integer with a persistence of 6 is: xxxx
The smallest integer with a persistence of 7 is: xxxxx

任何代码链接或线索都将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:-1)

def main():

    low_limit = 3
    high_limit = 7

    for limit in range(low_limit, high_limit+1):
        number = 1
        while persistence(number) < limit:
            number += 1
            print('The smallest integer with a persistence of', limit, 'is:', number)

def persistence(x):
    pcount = 0    # counting the p from 0
    while x>=10:
        y=x       # temp copy of x 
        z=1       # z holds the value of y as being broken down and multplied
        while (y!=0):
            z=z*(y%10)     # stripping last number and mult by last z value
            y=y//10       # integer division, dropping off the last digit
        x=z    
        pcount +=1
    return pcount

main()