i was attempting to write a python code that finds the largest prime factor in a given number, a for loop would count from 2 up, each time passing a number to a function that checks if it's a prime. nested in it is another for loop that sees if said given number can divide correctly with the current number in question, the code gives no results and seems to be stuck in an infinite loop.
here's the code :
def prime_x (n): #identify primes
for x in range (2,n):
if n % x == 0:
return False
return True
c = 600851475143 #given number
primes = []
for num in range(2 , c):
if prime_x (num):
if c % num == 0:
primes.append(num)
print (max(primes))
please note that i was able to find the answer by placing a print statement inside the loop, but i can't get the loop to end and the answer be printed on it's own.
ChatGPT tells me that the algorithm is not efficient and it causes the program to get stuck, however there was no memory error. additionally, when the mentioned in-loop solution is performed, outputs are displayed properly, indicating that the execution ran properly through the whole number set.