The Eratosthenes Sieve

We know the Eratosthenes Sieve as a method for selecting primes from the sequence of natural numbers. The algorithm of the sieve is very simple:

ES (for prime numbers)
  • Initialize: you write down the natural sequence starting from 2
  • Sieve: you repeatedly scan the base sequence proceeding from less to greater indexes, select the first number p you find unmarked and mark a number each p
  • Terminate: at any moment you interrupt the scan, you can select all the unmarked numbers less than the square t of the least of them. All the selected numbers are all the prime numbers up to t
  • As all the primes except 2 are odd, we would like to find an Eratosthenes-like Sieve for applying only to odd numbers. Such a sieve not only exists, but it is still the Eratosthenes Sieve, provided that we change the "Initialize" step in this way:

    ES for odd prime numbers
  • Initialize: you write down the 1 + 2 k sequence starting from 3
  • Sieve: you repeatedly scan the base sequence proceeding from less to greater indexes, select the first number p you find unmarked and mark a number each p
  • Terminate: at any moment you interrupt the scan, you can select all the unmarked numbers less than the square t of the least of them. All the selected numbers are all the prime numbers up to t
  • The ES practically didn't change because of the fact that each odd natural, that happens to be a multiple of a given odd prime p, is located (off from the previous multiple of p) at 2 p positions counting in the natural sequence, but only at p positions counting in the odd sub-sequence.
    So, marking in the natural sequence a number each p corresponds to pre-marking all the numbers of the 2 k sub-sequence and marking in the 1 + 2 k sub-sequence a number each p.

     

    We have to note that the "Sieve" step is highly optimized for the structure of the sequence given in the "Initialize" step, ie it is strictly bound to the concept of equally spaced terms in the base sequence.

    There is a method for selecting exactly the same odd primes that doesn't use that feature. Instead, the alternate method use the concept of coprimality, ie the fact that two numbers have no common factors except 1. Of course, this concept is closely related to the definition of a prime number.
    Testing two numbers for coprimality is not a difficult task and is far cheaper (in terms of computing resources) than testing a number for "simple" primality. In fact, testing for coprimality is as simple as calculating the Greatest Common Divisor (GCD) and, thanks to the Euclidean algorithm, the GCD is a simple task.
    If the two numbers are a and b, then a and b are coprimes iif GCD[ a, b ] == 1

    ES for odd prime numbers using coprimality
  • Initialize: you write down the 1 + 2 k sequence starting from 3
  • Sieve: you repeatedly scan the base sequence proceeding from less to greater indexes, select the first number p you find unmarked and mark each number n such that Not[ IsCoprime[ p, n ] ]
  • Terminate: at any moment you interrupt the scan, you can select all the unmarked numbers less than the square t of the least of them. All the selected numbers are all the prime numbers up to t
  •  

    The eXtended Eratosthenes Sieve

    The eXtended Eratosthenes Sieve is a method for selecting numbers from a given set of natural numbers. The starting set can be of any type, with or without a rule for determining any number of the set. By definition:

    XES (general)
  • Initialize: you write down the set
  • Sieve: you repeatedly scan the base set proceeding from less to greater indexes, select the first number p you find unmarked and mark each number n such that Not[ IsCoprime[ p, n ] ]
  • Terminate: all the selected numbers are all the coprime numbers of the set, scanning bottom-up.
  • Here is the formulation as a Mathematica function:

    XES[ set ]
    XES[ set_List ] :=
      Module[ {thisTerm, selectedTerms = {}, termsToSelect = set}, 
        While[ termsToSelect != {},
          thisTerm = First[ termsToSelect ];
          selectedTerms = {selectedTerms, thisTerm};
          termsToSelect = DeleteCases[ Rest[ termsToSelect ], eachTerm_ /; Not[ IsCoprime[ thisTerm, eachTerm ] ] ]
        ];
        Flatten[ selectedTerms ]
      ] /; IsNatural[ set ]
    

    It gives the set resulting from the application of the eXtended Eratosthenes Sieve to the natural set.

     

    If we know that the set is ordered and equally spaced then it is possible to optimize XES, just like ES, because of a nice property of that kind of sets.
    Let's name m the space between adjacent terms. If the n-th term of the set is equal to f g, then each (n + f k)-th term is equal to f g + f k m = f (g + k m), ie the "Sieve" step has to mark each (n + f k)-th term of the set, as well as each (n + g k)-th one.

    XES for equally spaced sets
  • Initialize: you write down the set
  • Sieve: you repeatedly scan the base set proceeding from less to greater indexes, select the first number p you find unmarked and, for each prime factor f of p, mark a number each f
  • Terminate: all the selected numbers are all the coprime numbers of the set, scanning bottom-up.
  • Here is the formulation as a Mathematica function:

    XES[ set ]
    pXES[ set_List ] :=
      Module[ {thisTerm, selectedTerms = {}, termsToSelect, primeFactors, thisFactor, len}, 
        len = Length[ set ];
        termsToSelect = Table[ True, {len} ];
        For[ t = 1, t <= len, t++,
          If[ termsToSelect[[ t ]],
            thisTerm = set[[ t ]];
            selectedTerms = {selectedTerms, thisTerm};
            primeFactors = First[ Transpose[ FactorInteger[ thisTerm ] ] ];
            While[ primeFactors != {},
              thisFactor = First[ primeFactors ];
              For[ i = t + thisFactor, i <= len, i += thisFactor,
                termsToSelect[[ i ]] = False
              ];
              primeFactors = Rest[ primeFactors ]
            ]
          ]    
        ];
        Flatten[ selectedTerms ]
      ]
    
    XES[ set_List ] := pXES[ set ]  /; IsNatural[ set ] && OrderedQ[ set ] && IsEquallySpaced[ set ]
    

    It gives the set resulting from the application of the eXtended Eratosthenes Sieve to the natural set.