hello, can someone plz tell me why this loop wont stop after finding the 10 first prime numbers? p=3 n=0 while (n<10): for a in range (1,999999): if(a**(p-1)-1)%p == 0: print p p=p+2 n=n+1
get the n=n+1 line inside the while loop...it will stop.. for now...n never increases from 0 , because n=n+1 is outside the while loop...so it is an infinite loop
so, do you mean like this: p=3 n=0 while (n<10): for a in range (1,999999): if(a**(p-1)-1)%p == 0: print p p=p+2 n=n+1 because that do not work
that won't coz it's inside the for loop.. try it like this p=3 n=0 while (n<10): for a in range (1,999999): if(a**(p-1)-1)%p == 0: print p p=p+2 n=n+1 because that do not work i'm not sure about your prime number method though..
No, he meant p=3 n=0 while (n<10): for a in range (1,999999): if(a**(p-1)-1)%p == 0: print p p=p+2 n=n+1 Blocks in python works by indentation. So if it's in a different level of indentation, it has a different meaning.
i found the method on wikipedia, but are you sure its like that. because when i run it like you show then it still keep on going past the 10th prime. sry for asking alot of questions but i am totally new to programming...
try this p=3 n=0 while (n<10): for a in range (1,99): if(a**(p-1)-1)%p == 0 and n<10: print p n=n+1 p=p+2
great, thanks :D but why did that work? is it not more intuitive to first write p=p+2 and then n=n+1?
notice i shortened the range...and the other thing the while loop is not checking for n here before the print actually...because it never exits for loop till it gets to the upper range..so u have to perform the check inside the for loop and therefore the increment has to be inside for loop too.. i am not any good at explaining things.. Lifeburner is pretty good at it though :)
At a glance, there are 2 possible problems there: 1) Like sunu pointed out, 99999 is just too big. 2) If I understand correctly, a is your divisor? If so, instead of just picking a very large upper limit, try setting a logical limit instead.
Join our real-time social learning platform and learn together with your friends!