No such thing as a small change PerlMonks

RE: RE: RE: sieve of erothenes

 on Jul 01, 2000 at 03:52 UTC ( #20697=note: print w/replies, xml ) Need Help??

in reply to RE: RE: sieve of erothenes
in thread sieve of erothenes

You win on the efficiency test too. with n = 100 we are roughly the same time, but as n gets large I get bogged down somewhere. I'm running a test with n = 1000000 on my dual proc, which has been hung for about five minutes now. But based on the out put I've already seen for smaller n's, I suspect that you will slaughter me time wise.

I don't understand why yours is faster because you use two for loops and I use a while loop the second time, dropping the overhead of making the temp array that foreach generates. Now I'm curious about the efficiencies of the two looping mechanisms...

Replies are listed 'Best First'.
RE: RE: RE: RE: sieve of erothenes
by maverick (Curate) on Jul 01, 2000 at 08:44 UTC

I suspect that it isn't my looping structure, but that I'm stepping across the primes low to high and you're going high to low. My code tests division by '2' first, so the inner loop only runs once half of the time. Thus only runs twice a third of the time, 4 times a fifth of the time, etc.

since you test the large numbers first, you have to make more tests to throw out a potential prime.

consider your test of 16:
16%13 -> 16%11 -> 16%7 -> 16%5 -> 16%3 -> 16%2 (not prime)
as opposed to mine:
16%2 (not prime)

The foreach may have temp array overhead, but it looks like I'm making up for it on number of % and iterations

/\/\averick

Although what I propose may not be the "classic" sieve algorithm, in truth you only need to test up to the primes which are less than the square root of the target number. In other words, you need not divide by any primes greater than 4 when testing 16. Based on the associative property of multiplication, if you can show that no numbers <= sqrt(n) evenly divide n, then you have also shown that there are no numbers => sqrt(n) which evenly divide n. While this may add a few chars to your code, it reduces the compute time enormously when you get to large n's.

Update: Actually, it doesn't look like ANY of us are implenting the true Sieve algorithm. The algorithm can be found here: http://www.math.utah.edu/~alfeld/Eratosthenes.html. It does in fact use the sqrt(n) modification, but doesn't do divide-testing ... instead it does "weeding out" of multiples. Same result, different technique.

Update 2: my mistake ... Ase is indeed doing the correct Sieve algorithm by using his bit vector technique.
You know, you are right. I had thought I had done it in the right order, but I didn't... I screwed that up. I guess I would have to use:
```@_=(1);L:for(2..100){\$i=0;while(@_>++\$i){next L if!(\$_%\$_[\$i])}push@_,
+\$_}
which uses 2 more chars. Sigh.

UPDATE: I can do it with one less char then above, but still too many.:

```@_=(1);L:for(2..100){\$i=-@_;while(++\$i){next L if!(\$_%\$_[\$i])}push@_,\$
+_}
Yes, I lie awake at night thinking about this stuff. No, not really. But when I get started thinking about it, I have a hard time stopping. But I doubt I'm alone in that disease. :)

Create A New User
Node Status?
node history
Node Type: note [id://20697]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2018-04-19 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (75 votes). Check out past polls.

Notices?