The description of the problem is:
The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
Here is my solution:
import heapq
from math import sqrt
def find_max_prime_factor(n: int) -> int:
'''
returns the largest prime factor of the non negative integer n
:param n: the number we want to find the max prime factor of
:return: the largest prime factor of n
'''
max_heap = []
# Checking for 2 as prime factor
if n % 2 == 0:
heapq.heappush(max_heap, -2)
# dividing by 2 until n becomes odd
while n % 2 == 0:
n = n/2
# dealing with odd numbers
for i in range(3,int(sqrt(n)),2):
if n % i == 0:
heapq.heappush(max_heap,-1*i)
while n % i == 0:
n = n/i
# dealing with case where n is a prime by itself
if n > 2:
return n
return heapq.heappop(max_heap) * (-1)
Here's some examples and test-cases:
from ProjectEuler.problem3 import find_max_prime_factor
if __name__ == "__main__":
# 13195 prime factors are 5,7,13,29 -> returns 29
print(find_max_prime_factor(13195))
# 600851475143 prime factors are 6857,1471,839,71 -> returns 6857
print(find_max_prime_factor(600851475143))
# 7 is prime therefor -> returns 7
print(find_max_prime_factor(7))
# 3452656 prime factors are 2,31,6961 -> returns 6961
print(find_max_prime_factor(3452656))
# 896776435 prime factors are 5,17,2549,4139 -> returns 4139
print(find_max_prime_factor(896776435))
Would love for feedback on my code.Thanks!