Program to compute and print 1000th prime number

View: New views
10 Messages — Rating Filter:   Alert me  

Program to compute and print 1000th prime number

by Ray Holt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

--
http://mail.python.org/mailman/listinfo/python-list

Re: Program to compute and print 1000th prime number

by ssteiner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.

S



--
http://mail.python.org/mailman/listinfo/python-list

Re: Program to compute and print 1000th prime number

by Robert P. J. Day-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 7 Nov 2009, ssteinerX@... 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
========================================================================
--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: Program to compute and print 1000th prime number

by Raymond Hettinger-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> > 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
--
http://mail.python.org/mailman/listinfo/python-list

Re: Program to compute and print 1000th prime number

by Robert P. J. Day-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 7 Nov 2009, Raymond Hettinger 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
>
> 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]
  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
========================================================================
--
http://mail.python.org/mailman/listinfo/python-list

Re: Program to compute and print 1000th prime number

by Xavier Ho :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Nov 8, 2009 at 12:44 AM, Ray Holt <mrholtsr@...> wrote:
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?

Ray, if you really want an answer out of this list, you'll have to at least show us what efforts you've put into solving this problem. Perhaps if you post your code that doesn't quite work, we can make suggestions on how to improve/fix it. Other than that, it's generally considered bad karma to give any kind of code, and we need to know where you are.

my 2c,
Xavier

--
http://mail.python.org/mailman/listinfo/python-list

Re: Program to compute and print 1000th prime number

by mensanator@aol.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Nov 7, 11:23 am, Raymond Hettinger <pyt...@...> 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
>
> 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]

Nice, but you can do better.

>>> import gmpy
>>> n = 1
>>> for i in xrange(1000):
        n = gmpy.next_prime(n)
>>> print n
7919

>
> Raymond

--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: Program to compute and print 1000th prime number

by John Posner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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/>



--
http://mail.python.org/mailman/listinfo/python-list

Re: Program to compute and print 1000th prime number

by Andre Engels :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Nov 7, 2009 at 6:40 PM, Mensanator <mensanator@...> wrote:

>> 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]
>
> Nice, but you can do better.
>
>>>> import gmpy
>>>> n = 1
>>>> for i in xrange(1000):
>        n = gmpy.next_prime(n)
>>>> print n
> 7919

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

n = 7919
print n


--
André Engels, andreengels@...
--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: Program to compute and print 1000th prime number

by Wayne Brehaut :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 7 Nov 2009 19:34:47 +0100, Andre Engels
<andreengels@...> wrote:

>On Sat, Nov 7, 2009 at 6:40 PM, Mensanator <mensanator@...> wrote:
>
>>> 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]
>>
>> Nice, but you can do better.
>>
>>>>> import gmpy
>>>>> n = 1
>>>>> for i in xrange(1000):
>>        n = gmpy.next_prime(n)
>>>>> print n
>> 7919
>
>With the help of the solutions given so far, I can do even better than that:
>
>n = 7919
>print n

>>> 7919
7919

?
--
http://mail.python.org/mailman/listinfo/python-list