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" that it will produce - >for

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
On 10 July 2013 17:15, Chris Angelico wrote: > 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

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 "next composite" that

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 a stdlib way to

Re: Prime number generator

2013-07-10 Thread Chris Angelico
On Thu, Jul 11, 2013 at 1:43 AM, Joshua Landau wrote: >> So, a few questions. Firstly, is there... > Of course there is. > >> Secondly, can the... > Of course it can. > >> Thirdly, is there... > Of course there is. I have no clue what, though. Heh, I guess I was asking for that kind of response :

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 composite" that it will pr

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