2017-02-26 44 views
1

我對學習python很有新意,並且在玩數學函數。我試圖創建一個功能,可以讓你找到某些權力,例如正方形,立方體。 爲什麼當我運行下面的代碼時,列出了大部分所需的權限,但卻錯過了一些權限。Python數學函數找不到範圍內給出的所有答案

def more_powers(): 
    print "For which power do you wish to find: " 
    power = int(raw_input("> ")) 

    print "Choose the upperbound: " 
    n = int(raw_input("> ")) 

    for num in range(2, n): 
     for base in range(2, num): 
      if log(num, base)/power == 1: 
       print "%d is a power of %d." % (num, base) 
      else: 
       base += 1 


For which power do you wish to find: 
> 3 
Choose the upperbound: 
> 5000 
8 is a power of 2. 
27 is a power of 3. 
64 is a power of 4. 
343 is a power of 7. 
512 is a power of 8. 
729 is a power of 9. 
1331 is a power of 11. 
1728 is a power of 12. 
2197 is a power of 13. 
2744 is a power of 14. 
3375 is a power of 15. 
4096 is a power of 16. 

正如你可以看到它錯過進行5,6,10和17

回答

2

Hint等效功率:

>>> log(125, 5)/3 == 1 
False 
>>> log(125, 5) 
3.0000000000000004 

>>> log(216, 6)/3 == 1 
False 
>>> log(216, 6) 
3.0000000000000004