Python  1.0
Functions | Variables
_04d_sieve Namespace Reference

The Sieve of Eratosthenes (276-194 BC). More...

Functions

def setDebug (stat)
 Set debugging on or off. More...
 
def sieve (n)
 Returns a list with all primes up to n. More...
 
def sieve2 (n)
 Memory efficient version, using bitarray. More...
 
def arrayF (n)
 Prepare an array for factorization. More...
 
def factorization (x, F)
 Return the factors of x, given an array with the smallest number that divides each integer (0, if the integer is prime) up to x. More...
 
def printArray (array)
 Print the primes corresponding to a given array. More...
 
def getArray (array)
 Return the primes corresponding to a given array. More...
 
def getTime (dsec)
 Return the time in the format: hr:min:sec:msec. More...
 
def main (argv=None)
 Main program for testing. More...
 

Variables

int MAXN = 32000000000
 Maximum integer value for n. More...
 
int MINN = 1000
 Value for switching to using sieve2. More...
 
bool debug__ = True
 Debugging state. More...
 

Detailed Description

The Sieve of Eratosthenes (276-194 BC).

Eratosthenes was invited by the Pharaoh Ptolemy III of Egypt, to be the chief librarian at the library of Alexandria, the most important in the ancient times.
He hold this position from 236 BC to his death.

The sieve is a simple, ancient algorithm for finding all prime numbers up to any given limit.
The algorithm iteratively marks as composite (i.e. not prime) the multiples of each prime, starting at 2, using only additions.

Author
Paulo Roma
Since
22/09/2009
See also
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
https://www.khanacademy.org/computing/computer-science/cryptography/comp-number-theory/v/sieve-of-eratosthenes-prime-adventure-part-4
Animation

Function Documentation

◆ arrayF()

def _04d_sieve.arrayF (   n)

Prepare an array for factorization.

For every number up to n, we will remember the smallest prime that divides this number.

For n = 20, return:

  [0, 0, 0, 0, 2, 0, 2, 0, 2, 3,  2,  0,  2,  0,  2,  3,  2,  0,  2,  0,  2]
   0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20
 
Parameters
nnumber to factorize.
Returns
array F.

Referenced by main().

◆ factorization()

def _04d_sieve.factorization (   x,
  F 
)

Return the factors of x, given an array with the smallest number that divides each integer (0, if the integer is prime) up to x.

If we know that one of the prime factors of x is p, then all the prime factors of x are p plus the decomposition of x/p.

Number x cannot have more than log x prime factors, because every prime factor is >= 2. Factorization by the above method works in O(log x) time complexity.

Note that consecutive factors will be presented in non-decreasing order. The amount of memory needed for large numbers is prohibitive.

  For x = 20,         F[20] = 2 -> [2]
      x = 20/2 = 10,  F[10] = 2 -> [2,2]
      x = 10/2 = 5,   F[5]  = 0 -> [2,2,5]
 
Parameters
xnumber to factorize.
Ffactor array.
See also
https://codility.com/media/train/9-Sieve.pdf
_10_factorize
_10_factorize2

Referenced by main().

◆ getArray()

def _04d_sieve.getArray (   array)

Return the primes corresponding to a given array.

Parameters
arrayArray of prime numbers.
Returns
list of prime numbers.

◆ getTime()

def _04d_sieve.getTime (   dsec)

Return the time in the format: hr:min:sec:msec.

Parameters
dsecnumber of seconds as a double.
Returns
a tuple (h, m, s, ms).

Referenced by main().

◆ main()

def _04d_sieve.main (   argv = None)

◆ printArray()

def _04d_sieve.printArray (   array)

Print the primes corresponding to a given array.

Parameters
arrayArray of prime numbers.
Returns
number of elements printed.

Referenced by main().

◆ setDebug()

def _04d_sieve.setDebug (   stat)

Set debugging on or off.

Parameters
statdebugging status.

Referenced by main().

◆ sieve()

def _04d_sieve.sieve (   n)

Returns a list with all primes up to n.

We're iterating through n numbers. Whenever we reach a prime p, we start iterating through the multiples of p up to n. For each prime, this takes n/p operations.

\( n\ \sum_{p\ prime\ \leq n} {{1} \over {p}} = n\ (ln ({ln {(n)}}) + M)\)

So we end up with the prime harmonic series up to n, which actually evaluates out to log(log(n)). The total complexity is then: O(n log(log(n)).

Parameters
na given positive integer.
Returns
a list holding the primes up to n.
See also
https://en.wikipedia.org/wiki/Divergence_of_the_sum_of_the_reciprocals_of_the_primes

References sieve2().

Referenced by main().

◆ sieve2()

def _04d_sieve.sieve2 (   n)

Memory efficient version, using bitarray.

For \(n = 1 \times 10^9\) (one billion).
Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz

  Number of primes: 50847534
  Number of bytes: 112
  Total Number of bytes: 160
  Run time: 0h:6m:42s:884ms
  Factors of  1000000000 is: [2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5]
  Number of bytes: 8000000080
  Total Number of bytes: 8000081728
  Run time: 0h:4m:5s:482m
 
Parameters
na given positive integer.
See also
https://pypi.org/project/bitarray/

Referenced by sieve().

Variable Documentation

◆ debug__

bool _04d_sieve.debug__ = True

Debugging state.

◆ MAXN

int _04d_sieve.MAXN = 32000000000

Maximum integer value for n.

◆ MINN

int _04d_sieve.MINN = 1000

Value for switching to using sieve2.