/
lib.php
62 lines (52 loc) · 1.34 KB
/
lib.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
54
55
56
57
58
59
60
61
62
<?php
/**
* lib.php
* Function library for palindromes script
* @author: Matt License (matt@mattlicense.co.uk)
* @date: 18 May 2013
*/
/**
* Function to determine whether an integer is a palindrome
* @param $int
* @return bool
* @throws InvalidArgumentException
*/
function is_palindrome($int)
{
if(!is_int($int)) throw new InvalidArgumentException($int." is not an integer");
$num = $int;
$reverse = 0;
while($num > 0) {
$remainder = $num%10;
$reverse = $reverse*10 + $remainder;
$num = ($num-$remainder)/10;
}
return $reverse == $int;
}
/**
* Function to generate the Sieve of Eratosthenes up to a limit ($int)
* @param $int
* @return array
*/
function eratosthenes($int)
{
$sieve = array();
$p = 2;
for($i = 2; $i <= $int; $i++) $sieve[] = $i; // fill an array from 2..$int
while($p < max($sieve)) {
$count = count($sieve);
for($i = 0; $i < $count; $i++) {
if(($sieve[$i] != $p) && ($sieve[$i] % $p == 0)) unset($sieve[$i]);
}
// shift the array index
$sieve = array_values($sieve);
// increment $p to the next integer in the array
for($j = 0; $j < count($sieve); $j++) {
if($p < $sieve[$j]) {
$p = $sieve[$j];
break;
}
}
}
return $sieve;
}