Created
February 12, 2019 07:19
-
-
Save gcapell/4309fdb0ee281e06bb71a9b694d81711 to your computer and use it in GitHub Desktop.
Given n, find a,b such that a**b = n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate all the primes (and quite a few non-primes, but meh) | |
def primeish(): | |
for n in 2,3,5: | |
yield n | |
base = 6 | |
while True: | |
yield base+1 | |
yield base+5 | |
base = base+6 | |
# Find prime factors of power (e.g. 144 is 2**4 x 3**2) | |
# If we find a factor with an exponent of 1, we've lost. | |
# If we find two factors where their exponents won't work | |
# together nicely (e.g. 2**3 * 7**5), we've lost. | |
# Otherwise, combine the factors together for our answer. | |
def isPP(power): | |
factors = [] | |
for p in primeish(): | |
if p*p > power: | |
return None | |
if power%p !=0: | |
continue | |
power, exp = extractPrimes(power, p) | |
if exp< 2: | |
return None | |
if not factors: | |
expGCD = exp | |
else: | |
expGCD = gcd(expGCD, exp) | |
if expGCD < 2: | |
return None | |
factors.append( (p, exp)) | |
if power == 1: | |
break | |
combinedP = 1 | |
# print factors | |
for p, exp in factors: | |
combinedP = combinedP * p ** (exp/expGCD) | |
return combinedP, expGCD | |
def gcd(x,y): | |
while (y): | |
x,y = y, x%y | |
return x | |
def extractPrimes(n, p): | |
exp = 0 | |
while n>1: | |
n2, rem = divmod(n,p) | |
if rem: | |
break | |
else: | |
n, exp = n2, exp+1 | |
return n, exp | |
print isPP(1800 ** 14) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment