You'r trying naive algorithm. Finding prime is one of basic elements of maths and programming. You may look at sieve of eratosthenes<http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes>and sieve of atkin <http://en.wikipedia.org/wiki/Sieve_of_atkin>
On Tue, Nov 17, 2009 at 3:35 AM, <christopher.h...@allisontransmission.com>wrote: > > > Kent Johnson <ken...@tds.net> wrote on 11/16/2009 04:00:02 PM: > > > > ---------- Forwarded message ---------- > > From: Ray Holt <mrhol...@sbcglobal.net> > > Date: Mon, Nov 16, 2009 at 1:55 PM > > Subject: RE: [Tutor] Help on finding the 1000th prime > > To: Kent Johnson <ken...@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 > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor