-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3.py
More file actions
35 lines (29 loc) · 864 Bytes
/
problem3.py
File metadata and controls
35 lines (29 loc) · 864 Bytes
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
'''
Largest prime factor
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143?
'''
from math import sqrt
THE_TARGET = 600851475143
# Solution 1
def divisors(upper_bound):
# Only yield odd numbers to cut search space in half.
for num in range(3, int(sqrt(upper_bound)), 2):
yield num
def get_upper_prime(target):
max_prime = 1
for num in divisors(target):
if target % num != 0:
continue
usda_prime = True
for divr in divisors(num):
if num % divr == 0:
# The number is NOT usda prime!
usda_prime = False
break
if usda_prime:
max_prime = num
return max_prime
if __name__ == '__main__':
print('The answer is {}'.format(get_upper_prime(THE_TARGET)))