Re: Sieve of Erastosthenes optimized to the max

Liste des GroupesRevenir à cl c++ 
Sujet : Re: Sieve of Erastosthenes optimized to the max
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.c++
Date : 02. Jun 2024, 12:23:24
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86ttibod7n.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Vir Campestris <vir.campestris@invalid.invalid> writes:

On 31/05/2024 06:17, Tim Rentsch wrote:
>
Vir Campestris <vir.campestris@invalid.invalid> writes:

[...]

I don't store even numbers.  That's an optimisation, and is of
course equivalent to Tim's Mod 30 trick, but with mod 2 instead.
And it doesn't require an divide which may be slow.
>
Like I keep saying, keeping the bits in a mod 30 representation
doesn't need any divides (at least not in the main loop, where
multiples are being computed).

[...]

I'm missing something.
>
When setting the bits for a multiple of a reasonably large prime
what I do is:
>
(1) Shift right 1 to drop the last bit.
>
(2) The remaining top bits tell me which word needs to be accessed.
I shift the multiple right to get the index of the word.
>
(3) The remaining bottom bits tell me which bit in the word needs
to be set, so I shift 1 left by those bottom bits to get a mask.
>
My operation (2) is a divide, by a power of 2.
>
Take 997 as an example:
multiple      mod30   div30
997           7       33
1994          14      66
2991          21      99
3988          28      132
4985          5       166
5982          12      199
6979          19      232
7976          26      265
8973          3       299
9970          10      332
[etc]
>
I don't see how you get that last column without a divide by 30.

Good example.  Let's look at this example more closely, first in
the a-bit-for-only-odd-numbers scheme.  Forgive me if any of the
following seems obvious.  Let me redo your table slightly:

 multiplier product       mod 2
    1         997          1
    2        1994          0
    3        2991          1
    4        3988          0
    5        4985          1
    6        5982          0
    7        6979          1
    8        7976          0
    9        8973          1
   10        9970          0
   11       10967          1
   [etc]

Which multipliers do we need to consider?  Obviously not 1.
After that, we don't need any even multipliers, since they
are all divisible by two, and there aren't even places in
the table where the product could go.  So we quickly get to
this:

 multiplier product       mod 2
    3        2991          1
    5        4985          1
    7        6979          1
    9        8973          1
   11       10967          1
   [etc]

Now which multipliers do we need?  We don't need 3 - we have
already crossed out all multiples of 3.  Similarly we don't
need 5, 7, or 11.  We don't need 9, because it has a factor
of 3, - any multiple of 9 is also a multiple of 3 and has already
been crossed out.  And so forth for 13, 15, 17, 19, 21, ...
A little thought should convince you we don't need to consider
any multiplier less than 997.  Let's pick up the table from
there (I have taken out 'product' and put in 'factors'):

 multiplier  factors
    997        997
    999        3 3 3 37
   1001        7 11 13
   1003        17 59
   1005        3 5 67
   1007        19 53
   1009        1009
   1011        3 337

We need to eliminate 997*997, as it certainly has not been
crossed out before.  We don't need to consider 999, 1001,
1003, 1005, 1007, or 1011, because they all have prime
factors less than 997, and so these products have already
been eliminated.  For 1009 though, it's prime, and has no
smaller prime factors, so we need to cross out 997*1009.
And so forth.  Speaking more generally, we need to consider
multipliers greater than 997 (as well as 997 itself) that
have not already been marked as composite.  We discover what
those numbers are by scanning the bitmap.  In pseudocode:

    for each prime p :
        starting at the bitmap location for p, and up
        for each not-yet-marked-composite m :
            mark as composite p*m
        end for
    end for

(I need to put in an asterisk here to say that this code
mostly works but misses some composites, and if you play
around with it you will see why.  But that isn't important
to my explanation so I'd like to pretend for now that
this code is what we need to look at (and in fact it
isn't far from code that actually does work).)

Now let's think about how things change when we are storing
bits only for numbers that are nonzero mod 30, so one block
of 30 per byte.  First it should be obvious that we need to
consider only those multipliers that are nonzero mod 30, or
in other words those numbers that correspond to a bit in the
table.  Pseudocode now looks something like this:

    for each index i in the table :
      for each bit in table[i] :
        if that bit has been marked composite : continue
        // we have found the next prime
        for each index j > i in the table :
          for each bit in table[j] :
            if that bit has beem marked composite : continue
            // we have found a multiplier m and ..
            // need to mark as composite p*m
          end
        end
      end
    end

The relevant values are:

    p == i*30 + x
    m == j*30 + y

where x and y are constant values that are nonzero mod 30,
that is, from the set { 1, 7, 11, 13, 17, 19, 23, 29 },
depending only on the respective bit positions for p and m.

The product p*m is what we're looking for, and in particular
p*m / 30 and p*m % 30.  Doing a little algebra

    p*m = (i*30 + x) * (j*30 + y)
        =  i*30 * j*30  +  i*30*y  +  j*30*x  +  x*y

The components of this last expression are

    p*m / 30 = 30*i*j + i*y + j*x + (x*y/30)
    p*m % 30 = (x*y%30)

In the pseudocode we said 'for each bit in table[...]', but
rather than use a for loop we write 8 specific tests, so the
values x and y are specific constants in each of the 8 cases.
Since they are constants, the values (x*y/30) and (x*y%30) can in
each of the 64 cases be computed as compile-time constants, and
so we eliminate the divide and remainder operations from run-time
by computing them at compile time.  Similarly the value (x*y%30)
can be turned into a bit mask at compile-time, because it's just
a long series of ?: expressions, one for each of the 8 values of
nonzero residues mod 30.

Note that the expression for p*m/30 gives the index in the
table for where to zap the bit for the product.

Were you able to follow all that?  Do you see how the scheme
works now?

I have deliberately left out explaining the problem of missing
some composite values and what to do about it, but I can
explain further if someone has trouble with that.

Date Sujet#  Auteur
23 Mar 24 * Re: Sieve of Erastosthenes optimized to the max68Chris M. Thomasson
23 Mar 24 `* Re: Sieve of Erastosthenes optimized to the max67Bonita Montero
23 Mar 24  `* Re: Sieve of Erastosthenes optimized to the max66Chris M. Thomasson
24 Mar 24   `* Re: Sieve of Erastosthenes optimized to the max65Bonita Montero
24 Mar 24    `* Re: Sieve of Erastosthenes optimized to the max64Chris M. Thomasson
24 Mar 24     `* Re: Sieve of Erastosthenes optimized to the max63Bonita Montero
24 Mar 24      `* Re: Sieve of Erastosthenes optimized to the max62Chris M. Thomasson
16 May 24       `* Re: Sieve of Erastosthenes optimized to the max61Vir Campestris
16 May 24        +- Re: Sieve of Erastosthenes optimized to the max1Ben Bacarisse
22 May 24        `* Re: Sieve of Erastosthenes optimized to the max59Tim Rentsch
30 May 24         `* Re: Sieve of Erastosthenes optimized to the max58Vir Campestris
30 May 24          +- Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
30 May 24          +* Re: Sieve of Erastosthenes optimized to the max3Paavo Helde
31 May 24          i`* Re: Sieve of Erastosthenes optimized to the max2Bonita Montero
31 May 24          i `- Re: Sieve of Erastosthenes optimized to the max1Paavo Helde
31 May 24          `* Re: Sieve of Erastosthenes optimized to the max53Tim Rentsch
1 Jun 24           `* Re: Sieve of Erastosthenes optimized to the max52Vir Campestris
2 Jun 24            +- Re: Sieve of Erastosthenes optimized to the max1Richard Damon
2 Jun 24            `* Re: Sieve of Erastosthenes optimized to the max50Tim Rentsch
3 Jun 24             `* Re: Sieve of Erastosthenes optimized to the max49Tim Rentsch
18 Jun 24              `* Re: Sieve of Erastosthenes optimized to the max48Vir Campestris
19 Jun 24               `* Re: Sieve of Erastosthenes optimized to the max47Tim Rentsch
30 Jun 24                `* Re: Sieve of Erastosthenes optimized to the max46Vir Campestris
2 Jul 24                 `* Re: Sieve of Erastosthenes optimized to the max45Tim Rentsch
2 Jul 24                  +- Re: Sieve of Erastosthenes optimized to the max1Vir Campestris
3 Jul 24                  `* Re: Sieve of Erastosthenes optimized to the max43Vir Campestris
15 Jul 24                   +- Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
20 Jul 24                   +* Re: Sieve of Erastosthenes optimized to the max40Tim Rentsch
25 Jul 24                   i`* OT: Re: Sieve of Erastosthenes optimized to the max39Vir Campestris
10 Aug 24                   i +* Re: OT: Re: Sieve of Erastosthenes optimized to the max36Tim Rentsch
12 Aug 24                   i i+* Re: OT: Re: Sieve of Erastosthenes optimized to the max2Vir Campestris
16 Aug 24                   i ii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
15 Aug 24                   i i`* Re: OT: Re: Sieve of Erastosthenes optimized to the max33Vir Campestris
16 Aug 24                   i i `* Re: OT: Re: Sieve of Erastosthenes optimized to the max32Tim Rentsch
16 Aug 24                   i i  +* Re: OT: Re: Sieve of Erastosthenes optimized to the max30Bonita Montero
16 Aug 24                   i i  i+- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
19 Aug 24                   i i  i+* Re: OT: Re: Sieve of Erastosthenes optimized to the max12Vir Campestris
20 Aug 24                   i i  ii`* Re: OT: Re: Sieve of Erastosthenes optimized to the max11Bonita Montero
20 Aug 24                   i i  ii `* Re: OT: Re: Sieve of Erastosthenes optimized to the max10Bonita Montero
20 Aug 24                   i i  ii  +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
20 Aug 24                   i i  ii  `* Re: OT: Re: Sieve of Erastosthenes optimized to the max8Vir Campestris
20 Aug 24                   i i  ii   +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
26 Aug 24                   i i  ii   `* Re: OT: Re: Sieve of Erastosthenes optimized to the max6Tim Rentsch
27 Aug 24                   i i  ii    `* Re: OT: Re: Sieve of Erastosthenes optimized to the max5Bonita Montero
1 Sep 24                   i i  ii     `* Re: OT: Re: Sieve of Erastosthenes optimized to the max4Vir Campestris
2 Sep 24                   i i  ii      `* Re: OT: Re: Sieve of Erastosthenes optimized to the max3Tim Rentsch
2 Sep 24                   i i  ii       +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
3 Sep 24                   i i  ii       `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Vir Campestris
19 Aug 24                   i i  i+* Re: OT: Re: Sieve of Erastosthenes optimized to the max12Vir Campestris
20 Aug 24                   i i  ii+* Re: OT: Re: Sieve of Erastosthenes optimized to the max4red floyd
20 Aug 24                   i i  iii+* Re: OT: Re: Sieve of Erastosthenes optimized to the max2Vir Campestris
26 Aug 24                   i i  iiii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
26 Aug 24                   i i  iii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
20 Aug 24                   i i  ii+* Re: OT: Re: Sieve of Erastosthenes optimized to the max3Bonita Montero
20 Aug 24                   i i  iii+- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
20 Aug 24                   i i  iii`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Bonita Montero
20 Aug 24                   i i  ii`* Re: OT: Re: Sieve of Erastosthenes optimized to the max4Bonita Montero
22 Aug 24                   i i  ii `* Re: OT: Re: Sieve of Erastosthenes optimized to the max3Vir Campestris
22 Aug 24                   i i  ii  `* Re: OT: Re: Sieve of Erastosthenes optimized to the max2Bonita Montero
22 Aug 24                   i i  ii   `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Vir Campestris
22 Aug 24                   i i  i`* Re: OT: Re: Sieve of Erastosthenes optimized to the max4Vir Campestris
23 Aug 24                   i i  i +* Re: OT: Re: Sieve of Erastosthenes optimized to the max2red floyd
26 Aug 24                   i i  i i`- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
26 Aug 24                   i i  i `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
19 Aug 24                   i i  `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
11 Aug 24                   i +- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
11 Aug 24                   i `- Re: OT: Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch
23 Jul 24                   `- Re: Sieve of Erastosthenes optimized to the max1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal