[Tutor] Prime numbers

Rafik Ouerchefani rafik at ubuntu.com
Sun Mar 28 10:16:02 CEST 2010


On Sat, Mar 27, 2010 at 11:08 PM, 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
>

you are dividing n by x so your while loop should be "while n <= 1000" right ?
here is the idea to find prime numbers.

loop n from 1 to 1000
     loop x from 1 to n
      if n % x == 0
           then increment counter
      if the counter is == 2, then x is dividable by only 1 and x => x
is a prime number


> 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.

well, asking here is cheating :-D

cheers,

-- Rafik


More information about the Tutor mailing list