[Tutor] Fwd: Help on finding the 1000th prime

christopher.henk at allisontransmission.com christopher.henk at allisontransmission.com
Mon Nov 16 23:05:44 CET 2009


Kent Johnson <kent37 at tds.net> wrote on 11/16/2009 04:00:02 PM:

> ---------- Forwarded message ----------
> From: Ray Holt <mrholtsr at sbcglobal.net>
> Date: Mon, Nov 16, 2009 at 1:55 PM
> Subject: RE: [Tutor] Help on finding the 1000th prime
> To: Kent Johnson <kent37 at tds.net>
> 
> 
> I hit the send button before I was ready. Here is the code that doesn't
> work.
        Not to nit pick, but try and copy the code exactly as you have it. 
 The code below has capitalization where it doesn't belong and indenting 
is not consistent, so there should be plenty of syntax errors if you tried 
to run it.  This makes it hard to figure out where your particular problem 
is.  Not sure if its a syntax problem or a algorithm problem.  Indentation 
is key in Python so without showing exactly what you have loops and 
branching is hard or impossible to follow.  This is also why it is good to 
include exactly what error you are experiencing.

> primeCount = 0
> primeCandidate = 2

        You might find it easier to start with a different primeCandidate 
and handle 2 as a special case.

> While primeCount <= 1000:
> isPrime = True
>        primeCandidate = primeCandidate + 2

        primeCandidate is going to be 4,6,8,10,12,14,16,18,...
        which won't yield many primes. see comment above

>        for x in range(2, primeCandidate):
>                if primeCandidate % x == 0:
>        print primeCandidate, "equals", x, "*", primeCandidate/x
>        isPrime = False
> Print primeCandidate
>        break
> If isPrime:
>        primeCount = primeCount + 2
> Print primeCandidate
> 

        It is hard to follow what is in which loop with the rest of the 
code, so I can't say much more on where you are having problems.  It looks 
like you have the parts there just a question of what goes where.

Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091116/d877dc3c/attachment-0001.htm>


More information about the Tutor mailing list