[Tutor] Prime numbers

Shashwat Anand anand.shashwat at gmail.com
Sat Mar 27 23:28:54 CET 2010


>>> [x for x in range(3,int((n**0.5)),2)]
[]
your while loop is an infinite loop. Had you read the range documentations ?

>>> range(3,int((n**0.5)),2)
[]
>>> n**0.5
1.7320508075688772
>>> n = 3
>>> n ** 0.5
1.7320508075688772
>>> int ( n ** 0.5)
1
>>> range ( 3, 1, 2)
[]


On Sun, Mar 28, 2010 at 3:38 AM, yd <ydmt923 at gmail.com> wrote:

>
> Having a problem finding the first 1000 prime numbers, here is my code:-
>
> print(2)
> n =3
> counter =1
> while counter <=1000:
>   for x in range(3,int((n**0.5)),2):
>     if n%x != 0:
>       print(n)
>       n+=1
>       counter+=1
>     else:
>       n+=1
>
> The problem is, it prints 2 and then does nothing, yet if i try and close,
> it says program is still running do you want to kill it, is there a way to
> do this with lists, i know python has a prime function but i am not going to
> use it because i want to solve this problem without 'cheating'.Thanks.
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100328/e5c48d85/attachment-0001.html>


More information about the Tutor mailing list