-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargest-prime-factor.py
More file actions
32 lines (27 loc) · 906 Bytes
/
largest-prime-factor.py
File metadata and controls
32 lines (27 loc) · 906 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
# The prime factors of 13195 are 5, 7, 13, and 29.
# What is the largest prime factor of the number 600851475143?
def findFactors(number):
listOfFactors = []
for i in range (1, int(number**0.5) + 1):
if number%i==0:
listOfFactors.append(i)
if i != number // i:
listOfFactors.append(number // i)
print(listOfFactors)
def isPrime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
primeFactors = [factor for factor in listOfFactors if isPrime(factor)]
return max(primeFactors) if primeFactors else None
print(findFactors(13195))
print(findFactors(600851475143))