[Tutor] noob

Alan Gauld alan.gauld at btinternet.com
Mon Nov 29 10:44:13 CET 2010


"Mary" <sharky5000 at comcast.net> wrote

> finding the 1000th prime

> The little build i did to find primes does fine until, for some 
> reason,
> 95 shows up, and later other multiples of 5 .....Is it me?
> Have uninstalled and reinstalled 2.7 twice.

It is always tempting when you start programming to assume
there must be something wrong with the tool. Sadly, it is virtually
never the case, it is the programmer at fault. :-)

I've added some questions/comments below...

> per = 25     # not sure why you start at 25...
> num = 3     # I'm assuming this is the number under test?
> yep = 0      # and this is the count of primes?
>
> while per > 0:
>    for den in range(2, num):      # den is denominator maybe?
>        if num % den == 0:         # this bit puzzles me.
>             num = num + 2
>        else: yep = (num)          # what do you think the 
> parentheses do here?
>    print yep
>    per = per - 1
>    num = num  + 2

I confess I don't follow the algorithm here, I'm not sure where the
primes are being stored, I don't know how you detect when you
have reached the 1000th. I'm not even sure there is a valid test
for a prime.

Try searching wikipedia, there are easier ways to find primes
and there are also easier ways to find a specific prime.
Getting the algorithm right is the first challenge, coding it
is the easy bit.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/






More information about the Tutor mailing list