Advertisement
phillip1882

perfectpower

Mar 14th, 2012
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. import math
  2.  
  3. def introot(value,root):
  4.    a = root
  5.    b = 0
  6.    while math.fabs(a-b) > 0:
  7.       b = a
  8.       a = int(1.0/root *((root-1.0)*a +value/a**(root-1.0)))
  9.    return a  
  10.        
  11. def isperfectpower(value):
  12.    minE,maxE,minB,maxB = 2,int(math.log(value,2)),2,introot(value,2)
  13.    while minB**maxE != value and maxB**minE != value and minE < maxE:
  14.          minE += 1
  15.          maxB = introot(value,minE)
  16.          minB += 1
  17.          maxE = int(math.log(value,minB))  
  18.          if minB**maxE == value or maxB**minE == value:
  19.             return True
  20.    return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement