Re: greatest multiple algorithm
- From: Phil Carmody <thefatphil_demunged@xxxxxxxxxxx>
- Date: 09 Jul 2007 13:08:19 +0300
"Rod Pemberton" <spamtrap@xxxxxxxxxx> writes:
I've tried to write a few trivial (odd) prime number programs over the
years. I'm curious as to how you guys think those two compare to some of
the trivial methods I've tried (esp. Bernstein).
integer array
array stores found primes
modulo with found primes in array to find new primes
slows down rapidly with increasing number of primes in array
consumes large amounts of memory
That doesn't sound like a sieve, it sounds like trial division.
You're not reusing the results of prior computations.
I always advise a sieve.
bit sieve
using single bits to represent odd numbers
finds primes upto memory_in_bytes*8*2
requires no modulo computation
requires many memory accesses
slows down based on size memory used
limited to available memory
You can use a wheel to decrease the memory requirements. Most
fast sieves use the 8 residues mod 30 as their wheel. That
almost halves the memory requirements. Even a wheel of 6 saves
you a third.
See DJB's eratosthenes as part of the primegen package.
James' sieve was a 30 too, IIRC.
brute computation
simple loop
i+=2 for number to test as a factor of the prime
j+=4*(i-1) for the square of i
i.e., to stop when i is roughly sqrt of prime being tested
skip 6k+3
test 6k-1 or 6k+1 modulo with i
fast, no real memory requirements
limited to cpu's largest integer size
Again, that sounds like trial division rather than a sieve.
The brute computation is been the fastest by far, but is obviously limited
to "small" primes.
TOS's (Tomas Oliveira e Silva) GPL-ed C source is acceptably fast,
and very simple. 50% speedup should be possible without too much
work, by introducing the concepts of a mod 6 wheel to either or
both of the small primes and the sieving region.
You won't get much faster without cranking out specifically
optimised inner loops for all the different possibilities.
(Which is what both JvB and TOS have done.)
I'll stop rambling - this has everything you need to know:
http://www.ieeta.pt/~tos/software/prime_sieve.html
Phil
--
"Home taping is killing big business profits. We left this side blank
so you can help." -- Dead Kennedys, written upon the B-side of tapes of
/In God We Trust, Inc./.
.
- Follow-Ups:
- Re: greatest multiple algorithm
- From: Rod Pemberton
- Re: greatest multiple algorithm
- References:
- Re: greatest multiple algorithm
- From: Bronson
- Re: greatest multiple algorithm
- From: Bronson
- Re: greatest multiple algorithm
- From: Bronson
- Re: greatest multiple algorithm
- From: Terence
- Re: greatest multiple algorithm
- From: Bronson
- Re: greatest multiple algorithm
- From: Jerome H. Fine
- Re: greatest multiple algorithm
- From: Phil Carmody
- Re: greatest multiple algorithm
- From: Rod Pemberton
- Re: greatest multiple algorithm
- Prev by Date: Get the FAQs
- Next by Date: Re: newbie: MUL product
- Previous by thread: Re: greatest multiple algorithm
- Next by thread: Re: greatest multiple algorithm
- Index(es):
Relevant Pages
|