2017-02-26 47 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