|
View:
New views
10 Messages
—
Rating Filter:
Alert me
|
|
|
Program to compute and print 1000th prime numberI 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 numberOn Nov 7, 2009, at 9:44 AM, Ray Holt wrote:
Copying code != doing an assignment. Try Knuth. S -- http://mail.python.org/mailman/listinfo/python-list |
|
|
Re: Program to compute and print 1000th prime numberOn 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. 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 |
|
|
|
|
|
Re: Program to compute and print 1000th prime numberOn 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] 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 numberOn Sun, Nov 8, 2009 at 12:44 AM, Ray Holt <mrholtsr@...> wrote:
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 numberOn 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 |
|
|
|
|
|
Re: Program to compute and print 1000th prime numberOn 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 |
|
|
|
| Free embeddable forum powered by Nabble | Forum Help |