-
Notifications
You must be signed in to change notification settings - Fork 0
/
factor.py
executable file
·52 lines (38 loc) · 1.02 KB
/
factor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import math
from collections import defaultdict
from eratosthenes import eratosthenes
def factor(N):
"""Factor a integer number N.
>>> factor(1)
defaultdict(<class 'int'>, {1: 1})
>>> factor(2)
defaultdict(<class 'int'>, {2: 1})
>>> factor(4)
defaultdict(<class 'int'>, {2: 2})
>>> factor(41)
defaultdict(<class 'int'>, {41: 1})
>>> factor(38556)
defaultdict(<class 'int'>, {17: 1, 2: 2, 3: 4, 7: 1})
>>> factor(1307674368000)
defaultdict(<class 'int'>, {17: 1, 2: 2, 3: 4, 7: 1})
"""
sqrt_n = int(math.sqrt(N))
factors = defaultdict(int)
if N == 1:
factors[1] = 1
return factors
# Find the primes that can be factors
primes = eratosthenes(N)
canstop = False
for p in primes:
while N % p == 0:
factors[p] += 1
N /= p
if p > sqrt_n:
canstop = True
if canstop:
break
return factors
if __name__ == "__main__":
import doctest
doctest.testmod()