problem003.py (656B)
1 # problemName = "Largest prime factor" 2 # problemNum = 3 3 # solutionBy = "FIGBERT" 4 # language = "Python" 5 # dateCompleted = "21/01/2020" 6 import math 7 8 def prime_factors(num): 9 primes = [] 10 factor_limit = int(math.sqrt(num)) + 1 11 while num % 2 == 0: 12 primes.append(2) 13 num = num / 2 14 for i in range(3, factor_limit, 2): 15 while num % i == 0: 16 primes.append(i) 17 num = num / i 18 if num > 2: 19 primes.append(num) 20 return primes 21 22 if __name__ == "__main__": 23 answer = max(prime_factors(600851475143)) 24 print( 25 "The largest prime factor of the number 600851475143 is %s" % answer 26 )