-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3.py
More file actions
75 lines (48 loc) · 1.45 KB
/
problem3.py
File metadata and controls
75 lines (48 loc) · 1.45 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
after 2 all prime factors are odd
check if number is odd? if yes, go down by 2 and check if that number is a factor.
Then is it prime.
"""
def isPrime(num):
divisor = int(num**0.5)
while divisor > 1:
if num%divisor == 0:
return False
divisor -= 1
return True
def findPrimeFactors(num):
max = num
start = 2
largestPrime = 0
while start < max:
if num%start == 0:
#finding pairs of factors and checking whether they are prime
if isPrime(start):
largestPrime = start
largerFactor = num/start
if isPrime(largerFactor):
return largerFactor
max = largerFactor #remaining factors mus be between start and its corresponding factor
start += 1
return largestPrime
print findPrimeFactors(13195)
print findPrimeFactors(600851475143)
#print findPrimeFactors(1238162376372637826) not fast enough for this... yet
"""
def findFactors(num):
nums = range(2,num+1)
factors = []
for i in nums:
if num%i == 0:
factors.append(i)
return factors
def isPrime(lst):
primeFactors = []
for i in lst:
if len(findFactors(i)) == 1:
primeFactors.append(i)
return primeFactors
print isPrime(findFactors(600851475143))
"""