Sieve of Eratosthenes In PHP

5th July 2013

The sieve of Eratosthenes is named after Eratosthenes of Cyrene who was a Greek mathematician who devised a mechanism to find a sequence of prime numbers using a simple algorithm.

Normally, looping through a list of numbers and finding the primes can be an expensive process. The seive of Eratosthenes is one of the most efficient way of working out all of the smaller prime numbers below (below 10 million or so).

The sieve works by looping through a list of consecutive numbers, starting at 2. For each number in the sequence the multiples of that number are marked to be removed from the list of numbers. When finished the numbers that are not marked are prime numbers.

This algorithm is pretty straightforward, but from that it is possible to create a simple PHP function that will generate all the prime numbers up to a given number.

  1. /**
  2.  * Find all the primes up to a given number using the seive of Eratosthenes algorythm.
  3.  *
  4.  * @param $finish The number to stop searching for prime numbers.
  5.  *
  6.  * @return array The array of prime numbers.
  7.  */
  8. function find_primes($finish) {
  9.  
  10. // Initialise the range of numbers.
  11. $number = 2;
  12. $range = range(2, $finish);
  13. $primes = array_combine($range, $range);
  14.  
  15. // Loop through the numbers and remove the multiples from the primes array.
  16. while ($number*$number < $finish) {
  17. for ($i = $number; $i <= $finish; $i += $number) {
  18. if ($i == $number) {
  19. continue;
  20. }
  21. unset($primes[$i]);
  22. }
  23. $number = next($primes);
  24. }
  25. return $primes;
  26. }

Here is an example of the function in use.

print_r(find_primes(20));

This generates the following output.

  1. Array
  2. (
  3. [2] => 2
  4. [3] => 3
  5. [5] => 5
  6. [7] => 7
  7. [11] => 11
  8. [13] => 13
  9. [17] => 17
  10. [19] => 19
  11. )

With a small amount of modification it is possible to create a function that will return an array containing a range of prime numbers. Because of the way in which the algorithm works is isn't possible to generate just a selection of numbers, the numbers must all be generated and then just the prime numbers needed must be split out of the array.

  1. /**
  2.  * Find all the primes up to a given number using the seive of Eratosthenes algorythm.
  3.  *
  4.  * @param $start The number to start searching for prime numbers.
  5.  * @param $finish The number to stop searching for numbers.
  6.  *
  7.  * @return array The array of prime numbers.
  8.  */
  9. function find_primes_range($start, $finish) {
  10. // Initialise the range of numbers.
  11. $number = 2;
  12. $range = range(2, $finish);
  13. $primes = array_combine($range, $range);
  14.  
  15. // Loop through the numbers and remove the multiples from the primes array.
  16. while ($number*$number < $finish) {
  17. for ($i = $number; $i <= $finish; $i += $number) {
  18. if ($i == $number) {
  19. continue;
  20. }
  21. unset($primes[$i]);
  22. }
  23. $number = next($primes);
  24. }
  25.  
  26. // Slice the array into the given range
  27. foreach ($primes as $prime) {
  28. if ($prime < $start) {
  29. unset($primes[$prime]);
  30. } else {
  31. break;
  32. }
  33. }
  34. return $primes;
  35. }

This can be used in the following way.

print_r(find_primes_range(20, 50));
  1. Array
  2. (
  3. [23] => 23
  4. [29] => 29
  5. [31] => 31
  6. [37] => 37
  7. [41] => 41
  8. [43] => 43
  9. [47] => 47
  10. )

 

Comments

Permalink
This wil produce a memory error when you try anything over 1 mil :( i was hoping to go from 2 to 100mil with this for a comparison.

Alex (Fri, 05/15/2015 - 18:51)

Permalink
To add to that, there may be a memory leak somewhere here

Alex (Fri, 05/15/2015 - 18:56)

Add new comment

The content of this field is kept private and will not be shown publicly.