Re: Prime number generator

2013-07-31 Thread Ian Kelly
On Tue, Jul 30, 2013 at 4:58 AM, Albert van der Horst wrote: > Notice that all values from i on are possibly present. > So you are better off with a list indexed by forthcoming i's and > each item containing a list of primes. What you do then, more or less, > is keep track of all dividers of prime

Re: Prime number generator

2013-07-30 Thread bryanjugglercryptographer
Chris Angelico wrote: > Bas wrote: > > Still trying to figure out your algorithm ... > > It's pretty simple. (That's a bad start, I know!) Like the Sieve of > Eratosthenes, it locates prime numbers, then deems every multiple of > them to be composite. Unlike the classic sieve, it does the "deem" >

Re: Prime number generator

2013-07-30 Thread Albert van der Horst
In article , Chris Angelico wrote: >And now for something completely different. > >I knocked together a prime number generator, just for the fun of it, >that works like a Sieve of Eratosthenes but unbounded. It keeps track >of all known primes and the "next composite" th

Re: Prime number generator

2013-07-10 Thread Joshua Landau
On 10 July 2013 19:56, Ian Kelly wrote: > On Wed, Jul 10, 2013 at 11:47 AM, Joshua Landau wrote: If you care about speed, you might want to check the heapq module. Removing the smallest item and inserting a new item in a heap both cost O(log(N)) time, while finding the minimum in

Re: Prime number generator

2013-07-10 Thread Ian Kelly
On Wed, Jul 10, 2013 at 11:47 AM, Joshua Landau wrote: >>> If you care about speed, you might want to check the heapq module. Removing >>> the smallest item and inserting a new item in a heap both cost O(log(N)) >>> time, while finding the minimum in a dictionary requires iterating over the >>>

Re: Prime number generator

2013-07-10 Thread Joshua Landau
rom heapq import heappush,heappop > i=2 > yield 2 > prime=[(2,2)] # Heap > while True: > smallest, prm = heappop(prime) > heappush(prime, (smallest+prm, prm)) > while i yield i >

Re: Prime number generator

2013-07-10 Thread Chris Angelico
On Thu, Jul 11, 2013 at 2:54 AM, Ian Kelly wrote: > As promised. Apologies for the excessive commenting. As noted, this > implementation is a recursive generator, which is done so that the > primes in the sieve can go only up to the square root of the current > prime, rather than tossing in ever

Re: Prime number generator

2013-07-10 Thread Ian Kelly
On Wed, Jul 10, 2013 at 10:16 AM, Ian Kelly wrote: > The other interesting thing about my sieve is that it's a recursive > generator. I'll dig it up later and share it. As promised. Apologies for the excessive commenting. As noted, this implementation is a recursive generator, which is done so

Re: Prime number generator

2013-07-10 Thread Chris Angelico
On Thu, Jul 11, 2013 at 2:01 AM, Steven D'Aprano wrote: > On Thu, 11 Jul 2013 00:00:59 +1000, Chris Angelico wrote: >> Thirdly, is there any sort of half-sane benchmark that I >> can compare this code to? And finally, whose wheel did I reinvent here? >> What name would this algorithm have? > > I c

Re: Prime number generator

2013-07-10 Thread Chris Angelico
On Thu, Jul 11, 2013 at 1:47 AM, bas wrote: > On Wednesday, July 10, 2013 5:12:19 PM UTC+2, Chris Angelico wrote: >> Well, that does answer the question. Unfortunately the use of lambda >> there has a severe performance cost [ ...] > If you care about speed, you might want to check the heapq modul

Re: Prime number generator

2013-07-10 Thread Ian Kelly
On Wed, Jul 10, 2013 at 8:00 AM, Chris Angelico wrote: > And now for something completely different. > > I knocked together a prime number generator, just for the fun of it, > that works like a Sieve of Eratosthenes but unbounded. It keeps track > of all known primes and the "

Re: Prime number generator

2013-07-10 Thread Steven D'Aprano
On Thu, 11 Jul 2013 00:00:59 +1000, Chris Angelico wrote: > And now for something completely different. > > I knocked together a prime number generator, just for the fun of it, > that works like a Sieve of Eratosthenes but unbounded. [...] > So, a few questions. Firstly, is there

Re: Prime number generator

2013-07-10 Thread Chris Angelico
he standard library, so I'd have to hunt that down. Presumably it's on PyPI. >> What name would this algorithm have? > I'll call it "the flamingo". Guess that's as good a name as any! > def generate_primes(): > """Generate an inf

Re: Prime number generator

2013-07-10 Thread bas
On Wednesday, July 10, 2013 5:12:19 PM UTC+2, Chris Angelico wrote: > Well, that does answer the question. Unfortunately the use of lambda > there has a severe performance cost [ ...] If you care about speed, you might want to check the heapq module. Removing the smallest item and inserting a new

Re: Prime number generator

2013-07-10 Thread Joshua Landau
On 10 July 2013 15:00, Chris Angelico wrote: > And now for something completely different. > > I knocked together a prime number generator, just for the fun of it, > that works like a Sieve of Eratosthenes but unbounded. It keeps track > of all known primes and the "next com

Re: Prime number generator

2013-07-10 Thread Chris Angelico
On Thu, Jul 11, 2013 at 12:35 AM, Bas wrote: > On Wednesday, July 10, 2013 4:00:59 PM UTC+2, Chris Angelico wrote: > [...] >> So, a few questions. Firstly, is there a stdlib way to find the key >> with the lowest corresponding value? In the above map, it would return >> 3, because 18 is the lowest

Re: Prime number generator

2013-07-10 Thread Bas
On Wednesday, July 10, 2013 4:00:59 PM UTC+2, Chris Angelico wrote: [...] > So, a few questions. Firstly, is there a stdlib way to find the key > with the lowest corresponding value? In the above map, it would return > 3, because 18 is the lowest value in the list. I want to do this with > a single

Prime number generator

2013-07-10 Thread Chris Angelico
And now for something completely different. I knocked together a prime number generator, just for the fun of it, that works like a Sieve of Eratosthenes but unbounded. It keeps track of all known primes and the "next composite" that it will produce - for instance, after yielding 13, the

Re: Infinite prime number generator

2010-06-29 Thread Thomas Mlynarczyk
Thomas Jollans schrieb: def primes(): yield 1 1 is not a prime number. Greetings, Thomas -- Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison! (Coluche) -- http://mail.python.org/mailman/listinfo/python-list

Re: Infinite prime number generator

2010-06-29 Thread John Posner
On 6/29/2010 12:51 PM, Thomas Jollans wrote: def rprimes(): def elim_mult(n): yield n for p in filter((lambda x:x%n != 0), elim_mult(n+1)): yield p yield 1 for p in elim_mult(2): yield p Thomas, take a look at the thread "Generators/iterators, Pythonicity, an

Infinite prime number generator

2010-06-29 Thread Thomas Jollans
I've been toying with Haskell a bit, and after implementing (essentially) the Sieve of Eratosthenes as an infinite list, thus: primes = 1 : foldr elim_mult [] [2..] where elim_mult n l = n : filter ((/=0) . (`mod` n)) l I wondered how easy it would be to do the same thing in Python. Obviously

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Wayne Brehaut
On Sat, 7 Nov 2009 19:34:47 +0100, Andre Engels wrote: >On Sat, Nov 7, 2009 at 6:40 PM, Mensanator wrote: > >>> Tongue in cheek solution: >>> >>> import urllib2 >>> >>> url = 'http://primes.utm.edu/lists/small/1.txt' >>> primes = [] >>> for line in urllib2.urlopen(url).read().splitlines(): >

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Andre Engels
On Sat, Nov 7, 2009 at 6:40 PM, Mensanator wrote: >> Tongue in cheek solution: >> >> import urllib2 >> >> url = 'http://primes.utm.edu/lists/small/1.txt' >> primes = [] >> for line in urllib2.urlopen(url).read().splitlines(): >>     values = line.split() >>     if len(values) == 10: >>      

Re: Program to compute and print 1000th prime number

2009-11-07 Thread John Posner
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

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Mensanator
ompute 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/s

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Xavier Ho
On Sun, Nov 8, 2009 at 12:44 AM, Ray Holt 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 u

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Robert P. J. Day
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

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Raymond Hettinger
> > 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

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Robert P. J. Day
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.

Re: Program to compute and print 1000th prime number

2009-11-07 Thread sstein...@gmail.com
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

Program to compute and print 1000th prime number

2009-11-07 Thread Ray Holt
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: Problem with returning prime number in a simple calculation program

2007-03-03 Thread Steven D'Aprano
quot;Number ")) > i = num - 1 > divcounter = 0 > while i > 1: > if num % i != 0: > divcounter += 1 > i -= 1 That's an inefficient algorithm, if it even works. I'm not sure that it works, and

Re: Problem with returning prime number in a simple calculation program

2007-03-03 Thread Bjoern Schliessmann
= num - 2: > print num, "is a prime number" > else: > print num, "is not a prime number" > [...] > As it says in the title, I'm having trouble with the prime number > function. It will print the sentence if the number is prime, but >

Problem with returning prime number in a simple calculation program

2007-03-03 Thread QHorizon
(): num = input("Number ") i = num - 1 divcounter = 0 while i > 1: if num % i != 0: divcounter += 1 i -= 1 if divcounter == num - 2: print num, "is a prime numbe

Re: Shortest prime number program

2006-02-13 Thread Tim Hochberg
In the spirit of pointless pessimization and obfuscation I have crushed something very similar to Alex Martelli's eratosthenes function onto a single line. It's truly monstrous, but somewhat entertaining [Some preemptive linebreaks added]: def short():import itertools as it;D={};g=D.get;return

Re: Shortest prime number program

2006-02-12 Thread Christoph Zwerschke
Tim Hochberg wrote: > from itertools import count, ifilter > def sieve(s=count(2)): > while 1:p=s.next();s=ifilter(p.__rmod__,s);yield p Nice! -- Christoph -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-12 Thread Tim Hochberg
While not nearly the shortest proposed thus far, I'm fond of: from itertools import count, ifilter def sieve(s=count(2)): while 1:p=s.next();s=ifilter(p.__rmod__,s);yield p It will generate quite a large number of primes before blowing up (at least 50,000 primes, p=611,957) and it's much fast

Re: Shortest prime number program

2006-02-11 Thread Ralf Muschall
Christoph Zwerschke wrote: > [EMAIL PROTECTED] schrieb: >> How about: >> [2]+[x for x in range(1,99) if 2**x%x==2] > If the range goes beyond 340, it also gives non-primes... [2,3]+[x for x in range(1,99) if 2**x%x==2 and 3**x%x==3] [2,3,5]+[x for x in range(1,99) if 2**x%x==2 and 3**x%x==3 and

Re: Shortest prime number program

2006-02-11 Thread swisscheese
> 42 Make that 41 and 40. -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread bearophileHUGS
This is a little shorter :-) [2]+[x for x in range(2,99)if 2**x%x==2] Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread swisscheese
> [2]+[x for x in range(1,99) if 2**x%x==2] 42 - brilliant! 41: [2]+[x for x in range(1,99)if 2**x%x==2] ... although it appears Christoph is right that it's not scalable. -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Jeffrey Schwab
[EMAIL PROTECTED] wrote: > swisscheese wrote: > >>r=range(2,99) >>m=[x*y for x in r for y in r] >>[x for x in r if not x in m] > > > How about: > > [2]+[x for x in range(1,99) if 2**x%x==2] 43. I'll be chewing on this one for a while. Thank you. :) -- http://mail.python.org/mailman/listinfo

Re: Shortest prime number program

2006-02-11 Thread Christoph Zwerschke
[EMAIL PROTECTED] schrieb: > How about: > > [2]+[x for x in range(1,99) if 2**x%x==2] If the range goes beyond 340, it also gives non-primes... -- Christoph -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread dickinsm
swisscheese wrote: > > r=range(2,99) > m=[x*y for x in r for y in r] > [x for x in r if not x in m] How about: [2]+[x for x in range(1,99) if 2**x%x==2] Mark -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Grant Edwards
On 2006-02-11, swisscheese <[EMAIL PROTECTED]> wrote: >>You can save two bytes with > > 56 - nice catch. > 55: > r=range(2,99) > [x for x in r if sum(x%d<1 for d in r)<2] And if this were FORTRAN: r=range(2,99) [xforxinrifsum(x%d<1fordinr)<2] ;) -- Grant Edwards grante

Re: Shortest prime number program

2006-02-11 Thread Ian Bygrave
On Sat, 11 Feb 2006 13:33:58 +, Ian Bygrave wrote: > Well, given a hypothetical new function 'sieve' which should have been: def sieve(f,l): if not l: return l head,tail=l[0],l[1:] def filter_func(x): return f(x,head) tail=filter(filter_func,tail) return [

Re: Shortest prime number program

2006-02-11 Thread Ian Bygrave
On Sat, 11 Feb 2006 12:43:23 +, Ian Bygrave wrote: > p,r=[],range(2,99) > while r:p,r=p+r[:1],[x for x in r if x%r[0]] > > And the result's in p. Well, given a hypothetical new function 'sieve' def sieve(f,l): if not l: return l head,tail=l[0],l[1:] def filter_func(x):

Re: Shortest prime number program

2006-02-11 Thread [EMAIL PROTECTED]
swisscheese wrote: > I figured someone out there must have written a minimal code size prime > number generator. I did not find one after a bit of searching around. > For primes up to 100 the best I could do was 70 characters (including > spaces): > > r=range(2,99) > m=[x*y

Re: Shortest prime number program

2006-02-11 Thread swisscheese
>You can save two bytes with 56 - nice catch. 55: r=range(2,99) [x for x in r if sum(x%d<1 for d in r)<2] -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Martin v. Löwis
You can save two bytes with r=range(2,99) [x for x in r if sum(x%d==0 for d in r)<2] -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Ian Bygrave
On Sat, 11 Feb 2006 02:03:46 -0800, swisscheese wrote: > I figured someone out there must have written a minimal code size prime > number generator. I did not find one after a bit of searching around. > For primes up to 100 the best I could do was 70 characters (including > spaces):

Re: Shortest prime number program

2006-02-11 Thread swisscheese
At 58, very nice :-) Building on yours we get 57: r=range(2,99) [x for x in r if sum([x%d==0 for d in r])<2] -- http://mail.python.org/mailman/listinfo/python-list

Re: Shortest prime number program

2006-02-11 Thread Mitja Trampus
swisscheese wrote: > I figured someone out there must have written a minimal code size prime > number generator. I did not find one after a bit of searching around. > For primes up to 100 the best I could do was 70 characters (including > spaces): > > r=range(2,99) > m=[x*y

Shortest prime number program

2006-02-11 Thread swisscheese
I figured someone out there must have written a minimal code size prime number generator. I did not find one after a bit of searching around. For primes up to 100 the best I could do was 70 characters (including spaces): r=range(2,99) m=[x*y for x in r for y in r] [x for x in r if not x in m

Re: prime number

2005-05-31 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, lostinpython <[EMAIL PROTECTED]> wrote: >It is a homework assignment from a book but not for a class. I'm >trying to teach my self some basic programming before I have to take it >in college. If I show enough understanding of the subject, my advisor >will let me fo

Alternative history (was: prime number)

2005-05-31 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Mike Meyer <[EMAIL PROTECTED]> wrote: . . . >If it isn't a homework assignment, and you're honestly in such, then >you should know there's been a lot of research in this area, because >primes ar

Re: prime number

2005-05-31 Thread Mikael Olofsson
lostinpython wrote: > What civil engineers need with all this programming is beyond me. We > have to learn another language to program our CADD software, which I > find much easier than this. According to my experience, almost every engineer - civil or not - uses programming at least occationa

Re: prime number

2005-05-30 Thread Dale Hagglund
Sigh ... one of my intermediate versions of is_prime() returns True if the n is *not* prime, and false otherwise. The final version is correct, though. Dale. -- http://mail.python.org/mailman/listinfo/python-list

Re: prime number

2005-05-30 Thread Dale Hagglund
lostinpython> I'm having trouble writing a program that figures lostinpython> out a prime number. Does anyone have an idea on how lostinpython> to write it? [I can't quite tell from your posts what your level of programming knowledge is, so I've aimed

Re: prime number

2005-05-30 Thread Rocco Moretti
lostinpython wrote: > But needless to say, I'm stumped on this > problem. I keep ending up in a never ending loop. I've been told that the trick with recursion/iteration is to always determine what your ending condition is first, and then construct the body of the recursion/loop to reach that e

Re: prime number

2005-05-30 Thread phil
> What civil engineers need with all this programming is beyond me. One of my best friends and expartner and top civil-structural engineers in the country (he built Dallas Reunion Arena and many other complex structures) was an ace programmer in many languages. He was not willing to just accep

Re: prime number

2005-05-30 Thread Brian van den Broek
lostinpython said unto the world upon 2005-05-30 02:50: > It is a homework assignment from a book but not for a class. I'm > trying to teach my self some basic programming before I have to take it > in college. If I show enough understanding of the subject, my advisor > will let me forgo Intro. t

Re: prime number

2005-05-29 Thread lostinpython
quot; <[EMAIL PROTECTED]> writes: > > I'm having trouble writing a program that figures out a prime number. > > Does anyone have an idea on how to write it? All I know is that n > 2 > > is prim if no number between 2 and sqrt of n (inclusivly) evenly > > divi

Re: prime number

2005-05-29 Thread Mike Meyer
"lostinpython" <[EMAIL PROTECTED]> writes: > I'm having trouble writing a program that figures out a prime number. > Does anyone have an idea on how to write it? All I know is that n > 2 > is prim if no number between 2 and sqrt of n (inclusivly) evenly >

Re: prime number

2005-05-29 Thread Tim Leslie
On 29 May 2005 19:55:32 -0700, lostinpython <[EMAIL PROTECTED]> wrote: > I'm having trouble writing a program that figures out a prime number. > Does anyone have an idea on how to write it? All I know is that n > 2 > is prim if no number between 2 and sqrt of n (inclusi

prime number

2005-05-29 Thread lostinpython
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n (inclusivly) evenly divides n. -- http://mail.python.org/mailman/listinfo/python-list