Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Computing the 1000th prime
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
MRAB  
View profile  
 More options Nov 12, 2:43 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Thu, 12 Nov 2009 19:43:38 +0000
Local: Thurs, Nov 12 2009 2:43 pm
Subject: Re: Computing the 1000th prime

The indentation starting from the second 'if'.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Nov 12, 3:07 pm
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Thu, 12 Nov 2009 15:07:29 -0500
Local: Thurs, Nov 12 2009 3:07 pm
Subject: Re: Computing the 1000th prime

You break on the first composite number, which means you immediately
exit the loop. Just let it fall through Also, a couple of things to
speed in up:

1) you look at all numbers from 2 to n to check if n is a prime
number. You only need to check from 2 to int(math.sqrt(n))

2) to really speed it up, keep a list of all the prime numbers. Then
you only need to check if a number is divisible by those

By combining the two, you'll only use a fraction of the comparisons.
For 23, you'd only loop twice (2 and 3) instead of 20 times to
determine that it's prime. The difference is even more dramatic on
large numbers.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Kaplan  
View profile  
 More options Nov 12, 3:11 pm
Newsgroups: comp.lang.python
From: Benjamin Kaplan <benjamin.kap...@case.edu>
Date: Thu, 12 Nov 2009 15:11:39 -0500
Local: Thurs, Nov 12 2009 3:11 pm
Subject: Re: Computing the 1000th prime
On Thursday, November 12, 2009, Benjamin Kaplan

Nevermind MRAB is right. I missed the indentation error there. I guess
that's what I get for  trying to evaluate code on my iPod touch
instead of getting my computer out and actually seeing what it's
doing.  >.<


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Nov 12, 3:51 pm
Newsgroups: comp.lang.python
From: Dave Angel <da...@ieee.org>
Date: Thu, 12 Nov 2009 15:51:44 -0500
Local: Thurs, Nov 12 2009 3:51 pm
Subject: Re: Computing the 1000th prime

There are a bunch of things wrong here.  Did you write this code, or was
it copied from somewhere else?  Because it looks like there are several
typos, that you could catch by inspection.

First, at what point in the loop do you decide that you have a prime?  
Why not add a print there, printing the prime, instead of printing a
value that's already been incremented beyond it.  And put labels on your
prints, or you'll never be able to decipher the output.  Chnage the
limit for PrimeCount to something small while you're debugging, because
you can figure out small primes and composites by hand.

Second, you have a loop which divides by x.  But you change the
PrimeCandidate within the loop, so it's not dividing the same value each
time through.  Check your indentation.

Third, your limit value is too high.  You aren't looking for 2000
primes, but 1000 of them.  Further, your count is off by 1, as this loop
doesn't identify 2 as a prime.

I'd recommend making this whole thing a function, and have it actually
build and return a list of primes.  Then the caller can check both the
size of the list and do a double check of the primality of each member.  
And naturally you'll be able to more readily check it yourself, either
by eye or by comparing it to one of the standard list of primes you can
find on the internet.

The function should take a count, and loop until the len(primelist)
matches the count.  Then just return the primelist.

DaveA


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google