2015-04-01 23 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()