Re: simple algorithm for finding primes
From: Paul Hsieh (qed_at_pobox.com)
Date: 12/10/03
- Next message: nrk: "Re: [OT] Re: Translate from c to asm"
- Previous message: Ed Morton: "Re: --(int)d"
- In reply to: someone else: "Re: simple algorithm for finding primes"
- Next in thread: Grumble: "Re: simple algorithm for finding primes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 10 Dec 2003 04:10:28 GMT
someone says...
> Paul Hsieh" <qed@pobox.com> wrote:
> > In article <3fd57e8e@funnel.arach.net.au> says...
> > > Sieve of Eratosthenes:
> > >
> > > Make a list of all the integers less than or equal to n (and greater
> > > than one). Strike out the multiples of all primes less than or equal to
> > > the square root of n. Then the numbers that are left are the primes.
> >
> > This discussion is probably better suited to something like
> > comp.programming. They are not much into algorithms or non-C related
> > subjects at all around here. But in any event, here's a nice little Sieve
> > of Eratosthenes for you:
> <snip>
> I tried the code from the link http://www.pobox.com/~qed/32bprim.c
> and indeed it's a little faster then mine (10%-15%)
Well it includes the Sieve of Eratosthenes within the source. It also includes
a kind of "parallel small prime divisor test" (using gcd!) before I bring out
the big guns (Strong-Pseduo-Prime test.) Given that you don't seem to care
about memory usage, you can easily crank the performance for small primes such
as all those < 10^6 by simply increasing the size of the sieve (QF_SIEVE_SIZE)
as desired.
> but it's pretty comlicated and requires a good understanding of both math
> and c, and like you said, is better suited to comp.programming, and unestly,
> i did not understand all of it.
Well, completely understanding it may be a tall order. But I give references
for the math-related algorithms, so you can study those at your leisure. What
is worth-while *understanding* in my algorithm is that it takes roughly the
same amount of time (once it gets past the sieve, and trivial divisors, I mean)
regardless of the size of the input.
Your algorithm is a fine, simplistic algorithm, however it really only works
because you determine the entire sequence of primes one after another. But
for longer and longer sequence of primes it will get slower and slower and
you'll run out of memory sooner or later.
-- Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/
- Next message: nrk: "Re: [OT] Re: Translate from c to asm"
- Previous message: Ed Morton: "Re: --(int)d"
- In reply to: someone else: "Re: simple algorithm for finding primes"
- Next in thread: Grumble: "Re: simple algorithm for finding primes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|