Make a list of small primes up to the suqare root of the number you want the primes to. (so if you want primes to 2^32, grab them to 2^16.)
Load up an array, call it 'counters'.
Set primes up the same way.
In counters shove your primes.
Set powarr as the two powers from 1 to 32
For i = 0 to n / 32Testbyte = ~0For p = 0 to number_of_Primes_in_listif counters /32 = i then 'You can do something to speed this up, like precomputing it in another table, but thats another story. Testbyte &= ~powarr[counters % i] //Get rid of that byte counters = counters + primes 'Move the counters to the next oneend ifIf testbyte = 0 then goto next_loop 'If there are no more possible primes,' then why bother testing any more? I know goto's are evil, but this is the only' way i can do itNext pFor tmp = 1 to 32 'Check the bits on the tmpbyte to see if we have anything.if testbyte & powarr[tmp] then print i * 32 + tmp " Is a PRIME!!!"end ifnext tmpnext_loop:Next i
This code should work (well, its psudocode), but i'm not making any guarentees.
Simply, its letting you sieve a bit slower, but with o(sqr(n)) memory. (which will help heaps if you ever want to find every prime to 2^64).
You might be able to speed it up quite a bit some some 'upgrades'. (things like storing counters
/32 when you make it.)
Hth.
From,
Nice coder