Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Program to compute and print 1000th prime number
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
  7 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
 
Robert P. J. Day  
View profile  
 More options Nov 8, 2:21 am
Newsgroups: comp.lang.python
From: "Robert P. J. Day" <rpj...@crashcourse.ca>
Date: Sat, 7 Nov 2009 10:21:40 -0500 (EST)
Local: Sun, Nov 8 2009 2:21 am
Subject: Re: Program to compute and print 1000th prime number

On Sat, 7 Nov 2009, sstein...@gmail.com wrote:

> On Nov 7, 2009, at 9:44 AM, Ray Holt wrote:

>       I am taking the MIT online course Introduction to Computer Science and
>       Programming. I have a assignment to write a program to compute and print
>       the 1000th. prime number. Can someone give me some leads on the correct
>       code? Thanks, Ray

> Copying code != doing an assignment.  Try Knuth.

  i was going to say much the same, but it's also worth pointing out
that, using standard techniques, there is no straightforward way to
print the n'th prime number, given some initial value of n.

  the ubiquitous sieve of eratosthenes requires you to pre-specify
your maximum value, after which -- once the sieve completes -- all you
know is that you have all of the prime numbers up to n.  whether
you'll have 1000 of them isn't clear, which means that you might have
to start all over with a larger maximum value.  (being able to
directly determine the n'th prime number would solve a *lot* of prime
number problems. :-)

  and given that one can google and, in seconds, have the solution, i
feel no guilt in referring to
http://code.activestate.com/recipes/366178/.

rday
--

========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================


    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.
Raymond Hettinger  
View profile  
 More options Nov 8, 4:23 am
Newsgroups: comp.lang.python
From: Raymond Hettinger <pyt...@rcn.com>
Date: Sat, 7 Nov 2009 09:23:06 -0800 (PST)
Local: Sun, Nov 8 2009 4:23 am
Subject: Re: Program to compute and print 1000th prime number

> > On Nov 7, 2009, at 9:44 AM, Ray Holt wrote:

> >       I am taking the MIT online course Introduction to Computer Science and
> >       Programming. I have a assignment to write a program to compute and print
> >       the 1000th. prime number. Can someone give me some leads on the correct
> >       code? Thanks, Ray

Tongue in cheek solution:

import urllib2

url = 'http://primes.utm.edu/lists/small/10000.txt'
primes = []
for line in urllib2.urlopen(url).read().splitlines():
    values = line.split()
    if len(values) == 10:
        primes.extend(values)
print primes[1000-1]

Raymond


    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.
Robert P. J. Day  
View profile  
 More options Nov 8, 4:32 am
Newsgroups: comp.lang.python
From: "Robert P. J. Day" <rpj...@crashcourse.ca>
Date: Sat, 7 Nov 2009 12:32:45 -0500 (EST)
Local: Sun, Nov 8 2009 4:32 am
Subject: Re: Program to compute and print 1000th prime number

  reminds me of a variation of an old joke:  using nothing but this
barometer, determine the height of that building.

  answer:  go to the building manager and say, "i'll give you this
really neat barometer if you tell me how tall this building is."

rday
--

========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================


    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.
Mensanator  
View profile  
 More options Nov 8, 4:40 am
Newsgroups: comp.lang.python
From: Mensanator <mensana...@aol.com>
Date: Sat, 7 Nov 2009 09:40:31 -0800 (PST)
Local: Sun, Nov 8 2009 4:40 am
Subject: Re: Program to compute and print 1000th prime number
On Nov 7, 11:23 am, Raymond Hettinger <pyt...@rcn.com> wrote:

Nice, but you can do better.

>>> import gmpy
>>> n = 1
>>> for i in xrange(1000):

        n = gmpy.next_prime(n)
>>> print n

7919


    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.
John Posner  
View profile  
 More options Nov 8, 5:18 am
Newsgroups: comp.lang.python
From: John Posner <jjpos...@optimum.net>
Date: Sat, 07 Nov 2009 13:18:18 -0500
Local: Sun, Nov 8 2009 5:18 am
Subject: Re: Program to compute and print 1000th prime number
Robert P. J. Day said:
>   the ubiquitous sieve of eratosthenes requires you to pre-specify
> your maximum value, after which -- once the sieve completes -- all you
> know is that you have all of the prime numbers up to n.  whether
> you'll have 1000 of them isn't clear, which means that you might have
> to start all over with a larger maximum value.  (being able to
> directly determine the n'th prime number would solve a *lot* of prime
> number problems. :-)

In April of this year, members of this forum helped me to devise a
prime-number iterator [1]. So here's a simple solution for the OP:

#---------------
from itertools import ifilter, count

# create iterator
prime_iter = ifilter(
    lambda n, P=[]: all(n%p for p in P) and not P.append(n),
                    count(2))

# throw away lots of primes
for i in range(999):  
    prime_iter.next()

# here's the one we want
print prime_iter.next()      #7919
#---------------

I don't think this is a solution that a course instructor would expect,
though!

As mentioned in [1], optimizations of this algorithm (using
itertools.takewhile() and/or caching previously-computed squares) are
still at cl1p.net [2].

-John

[1]  http://mail.python.org/pipermail/python-list/2009-April/177415.html

[2]  http://www.cl1p.net/python_prime_generators

 <http://cl1p.net/python_prime_generators/>


    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.
Andre Engels  
View profile  
 More options Nov 8, 5:34 am
Newsgroups: comp.lang.python
From: Andre Engels <andreeng...@gmail.com>
Date: Sat, 7 Nov 2009 19:34:47 +0100
Local: Sun, Nov 8 2009 5:34 am
Subject: Re: Program to compute and print 1000th prime number

With the help of the solutions given so far, I can do even better than that:

n = 7919
print n

--
André Engels, andreeng...@gmail.com


    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.
Wayne Brehaut  
View profile  
 More options Nov 8, 9:43 am
Newsgroups: comp.lang.python
From: Wayne Brehaut <wbreh...@mcsnet.ca>
Date: Sat, 07 Nov 2009 15:43:13 -0700
Local: Sun, Nov 8 2009 9:43 am
Subject: Re: Program to compute and print 1000th prime number
On Sat, 7 Nov 2009 19:34:47 +0100, Andre Engels

7919

?


    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