Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. def factorize( n ):
  2.     assert type(n) in (int, long)
  3.     assert n > 0
  4.     div = 2
  5.     while div <= n:
  6.         if n % div == 0:
  7.             n /= div
  8.             yield div
  9.         else:
  10.             div += 1
  11.  
  12. def gcd( a, b ):
  13.     while b > 0:
  14.         a, b = b, a % b
  15.     return a
  16.  
  17. def count_values( values ):
  18.     res = {}
  19.     for value in values:
  20.         if not value in res:
  21.             res[ value ] = 0
  22.         res[ value ] += 1
  23.     return res
  24.  
  25. def whatpower(n):
  26.     if 1 == n: return (1,1)
  27.     fc = count_values( factorize( n ) )
  28.     p = reduce( gcd, fc.values() )
  29.     b = reduce( lambda x,y: x*y, map( lambda x:(x[0]**(x[1]/p)), fc.items()) )
  30.     return (b,p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement