Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

perfectpower

By: phillip1882 on Mar 14th, 2012  |  syntax: Python  |  size: 0.57 KB  |  views: 57  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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