-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpe3.py
More file actions
60 lines (53 loc) · 1.45 KB
/
pe3.py
File metadata and controls
60 lines (53 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
##############################################################################
#
# Problem 3
#
# The prime factors of 13195 are 5, 7, 13 and 29.
#
# What is the largest prime factor of the number 600851475143 ?
#
# Ans: 6857
##############################################################################
def isPrime(num: int) -> bool:
'''
Args:
num (int): number to check if prime
Returns:
(bool): True if prime, false if not
'''
if num < 1:
raise ValueError("Please enter a natural number.")
elif num == 2:
return True
elif num % 2 == 0:
return False
else:
for i in range(2, num//2+1): # Check if i divides the first half of num
if num % i == 0:
return False
return True
def listProduct(nums):
'''
Args:
nums (list): list of integers
Returns:
prod (int): product of all list elements
'''
prod = 1
for i in nums:
prod *= i
return prod
def getPrimeFactors(n=600851475143):
i = 2
factors = []
temp = n
# Iterate until list of factors multiples to n
while listProduct(factors) != n:
if temp % i == 0: # If i is a factor then append
factors.append(i)
temp //= i # update by dividing by i
else:
i += 1 # i is not a factor so check next
return factors
if __name__ == "__main__":
print(getPrimeFactors()[-1])