-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003.py
47 lines (39 loc) · 1.09 KB
/
003.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
import math
def isEven(n):
return ((n % 2) == 0)
def isPrime(n):
if isEven(n) and n != 2:
return False
if n >= 3:
for x in range(3, math.floor(n / 2), 2):
if ((n % x) == 0):
return False
elif n == 2:
return True
else:
return False
return True
def findPrimeFactors(n):
pf = []
for x in range(2, math.floor(n / 2), 1):
if ((n % x) == 0):
if isPrime(x):
pf.append(x)
d = math.floor(n / x)
if isPrime(d):
pf.append(d)
else:
pf = pf + findPrimeFactors(d)
else:
pf = pf + findPrimeFactors(x)
#breakout if pf values equal n
sum = 1
for f in pf:
sum = sum * f
if sum == n:
return pf
return pf
#print(findPrimeFactors(12)) # expecting [2, 2, 3]
#print(findPrimeFactors(147)) # expecting [3, 7, 7]
#print(findPrimeFactors(13195)) # expecting [5, 7, 13, 29]
print(findPrimeFactors(600851475143))