Last active
January 17, 2019 16:20
-
-
Save mateusza/3ad2e7d990b90c45b0301e39e640341e to your computer and use it in GitHub Desktop.
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
import sys | |
import fractions | |
import operator | |
import collections | |
def factorize( n ): | |
assert type(n) in (int, long) | |
assert n > 0 | |
div = 2 | |
while n > 1: | |
if 0 == n % div: | |
n /= div | |
yield div | |
else: | |
div += 1 | |
def whatpower(n): | |
if 1 == n: return (1,1) | |
fc = collections.Counter( factorize( n ) ) | |
p = reduce( fractions.gcd, fc.values() ) | |
b = reduce( operator.mul, map( lambda x:(x[0]**(x[1]/p)), fc.items()) ) | |
return (b,p) | |
if __name__ == '__main__': | |
if len( sys.argv ) <= 1: | |
print "What number?" | |
sys.exit() | |
try: | |
print "%d^%d" % whatpower( long( sys.argv[1] ) ) | |
except: | |
print "Wrong value: %s" % sys.argv[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment