in reply to Re^3: Math help: Finding Prime Numbers
in thread Math help: Finding Prime Numbers

Ovid said:

This is part of a larger set of code which is intended to be pure Perl, ...

Can you write a "half descent sieve" in Perl?

Also, how about if you want the 2e6 through 3e6 primes the first time you call and the 1e6 through 2e6 the second time?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^5: Math help: Finding Prime Numbers
by syphilis (Archbishop) on Nov 13, 2006 at 22:33 UTC
    I wasn't responding to Ovid. I was responding to Anonymous Monk's thoughts on sieving vs reading from disk - which I took to be of a more general nature (ie not in relation to just "pure perl").

    I honestly don't know whether a "pure perl" sieve would beat reading from disk. I might give it a whirl later today, if I get time. (Or has somebody else already done compararive benchmarking ?)

    Cheers,
    Rob

      Using a fairly highly tuned C implementation of a Sieve of Atkin prime generator, finding the 15 millionth prime takes just over one second. Using my load and lookup Perl takes just under to do the same thing.

      I think it doubtful that a pure perl implementation could achieve anywhere near this performance, as the C implementation is using hand-coded optimisations for 32-bit math--unwinding loops; using lookups instead of division; tailoring to L1 cache sizes etc. But you might prove me wrong :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Yes - primegen is very impressive .... and so is your technique for storing and retrieving them :-)

        But how does that relate to the statement I was addressing - namely "Considering that lists of prime numbers are easily available, I'm not so sure whether a sieve beats reading them from disk" ? To me, it's fairly apparent that Anonymous Monk was thinking of getting a list of prime numbers, putting them in a flat text file, and reading that file into memory whenever the list of primes is needed. I was simply clarifying for him that the sieve is faster.

        I guess it's possible that I'm selling Anonymous Monk short .... perhaps the storage/retrieval technique that he envisaged was more sophisticated and efficient than I gave him credit for - something like yours, for example. (If so ... my apologies to Anonymous Monk.)

        Cheers,
        Rob