2015-04-01 56 views
-3

需要一些帮助来在Python中编写一个Persistent number程序来确定持续性为 (715 ---> 35 ---> 15 ---> 5)为3的最小非负整数,然后如图4所示,然后如图5所示,然后如图6所示,然后7.具有下列格式的输出:Python持久号码

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 

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

+2

这本来是有帮助的,以提供一个什么样的数量的持久性是一个解释的解释或[链接](http://en.wikipedia.org/wiki/Persistence_of_a_number),而不是仅仅提供不可思议的715→35→15→5序列。另外,如果你认为这个网站是一个供人们为你提供代码的地方,那么你的网站就是错误的。 – user2357112 2015-04-01 00:27:27

+6

重复[http://stackoverflow.com/questions/7973690/computing-persistence-number-of-an-integer](http://stackoverflow.com/questions/7973690/computing-persistence-number-of-an -integer) – koukouviou 2015-04-01 00:30:57

+0

@ user2357112:感谢您的链接。我只看到了之前的增加数字类型的持久性。我不知道还有其他类型的,比如OP所指的多位数版本。 – 2015-04-01 05:37:36

回答

-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()