-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSieve_of_Eratosthenes.php
53 lines (44 loc) · 1.34 KB
/
Sieve_of_Eratosthenes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Sieve of Eratosthenes
* compute all the prime numbers that are smaller than or equal to n.
* holds numbers from 2 to n (by index).
* 1. $sieve[$k] == false means that $k is prime.
* 2. $sieve[$k] != false means that $k is composite,
* and $sieve[$k] is one of his prime factors.
*
* Time Complexity: O(n * log(log(n)))
*/
function sieveOfEratosthenes($n) {
$sieve = array_fill(2, $n - 1, false);
for ($p = 2; $p * $p <= $n; $p ++) {
if (! $sieve[ $p ]) {
for ($i = $p * $p; $i <= $n; $i += $p)
$sieve[ $i ] = $p;
}
}
return $sieve;
}
// ----------------
// --- Examples ---
// ----------------
define("NL", PHP_SAPI === 'cli' ? "\n" : "<br />"); // define new line feed based on web/cli
function getPrimes($n) {
return array_keys(
array_filter(sieveOfEratosthenes($n), function ($p) {
return (! $p);
})
);
}
$n = 1;
$primes = getPrimes($n);
printf("sieveOfEratosthenes(%d) = [%s]" . NL, $n, implode(', ', $primes));
$n = 20;
$primes = getPrimes($n);
printf("sieveOfEratosthenes(%d) = [%s]" . NL, $n, implode(', ', $primes));
$n = 50;
$primes = getPrimes($n);
printf("sieveOfEratosthenes(%d) = [%s]" . NL, $n, implode(', ', $primes));
$n = 100;
$primes = getPrimes($n);
printf("sieveOfEratosthenes(%d) = [%s]" . NL, $n, implode(', ', $primes));