I've been trying to solve project Euler problem #3 for a while now. The code below still doesn't work the way I want it to.
Question 3: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
getal = 13195
x = 2
while x in xrange(2,getal):
if getal % x == 0:
num = getal / x
for i in xrange (2,num):
if num % i == 0:
x += 1
break
else:
print num
else:
x += 1
continue
As you can see I'm running it right now with 13195 for the sake of simplicity, but eventually it should have to work with the bigger number.
My output is this:
2639 2639 2639 2639 2639 1885 1885 1885 1015 1015 1015 455 455 455 377
377 377 377 377 377 377 377 377 377 377 203 203 203 203 203 145 145
145 91 91 91 91 91 65 65 65 35 35 35 29 29 29 29 29 29 29 29 29 29 29
29 29 29 29 29 29 29 29 29 29 29 29 29 29
29 goes on for a while longer than I showed. I do understand it just prints the number as soon as it has found an "i" that this number cannot be divided by. But I don't know how to prevent it from doing this. Because in the end 29 is the right answer, however it should just give this answer once and right away.