Re: Program to compute and print 1000th prime number
by Mensanator other posts by this author
Nov 7 2009 9:47AM messages near this date
Re: Program to compute and print 1000th prime number
|
Re: Program to compute and print 1000th prime number
On Nov 7, 11:23Â am, Raymond Hettinger <pyt...@[...].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
>
> 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
Thread:
Ray Holt
Xavier Ho
Wayne Brehaut
John Posner
Mensanator
Andre Engels
ssteinerX@gmail.com
Raymond Hettinger
Robert P. J. Day
Robert P. J. Day
|