Last active
August 29, 2015 14:14
-
-
Save cesarkawakami/9e395b51c3ffe36deec5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
cesarkawakami@pandambp13r:~/temp/TheBigDeal [crap3] | |
$ cat factor.py | |
import collections | |
import math | |
def factor(n): | |
factors = collections.defaultdict(int) | |
for candidate in range(2, int(math.sqrt(n))): | |
if n == 1: | |
break | |
while n % candidate == 0: | |
n /= candidate | |
factors[candidate] += 1 | |
assert n == 1 | |
return factors | |
if __name__ == "__main__": | |
print(factor(600851475143)) | |
cesarkawakami@pandambp13r:~/temp/TheBigDeal [crap3] | |
$ time python3 factor.py | |
defaultdict(<class 'int'>, {6857: 1, 839: 1, 1471: 1, 71: 1}) | |
real 0m0.026s | |
user 0m0.020s | |
sys 0m0.005s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment