/
pe14.php
59 lines (41 loc) · 853 Bytes
/
pe14.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
<style>
body{
font-family:sans-serif;
}
</style>
<?php
ini_set("display_errors", TRUE);
ini_set('max_execution_time', 60*5);
function nextCollatz($current){
$next = -1;
if($current % 2 == 0){
$next = $current / 2;
}
else{
$next = 3 * $current + 1;
}
return $next;
}
function collatz($seed){
$length = 1;
$next = nextCollatz($seed);
while($next > 1){
$next = nextCollatz($next);
$length += 1;
}
return $length + 1;
}
echo("<a href='https://projecteuler.net/problem=14'>Project Euler - Problem 14</a>");
echo("<br>");
echo("<br>");
$lengths = array();
$start = 77031;
for($i=$start; $i<1000000; $i++){
$seed = $i;
$lengths[] = collatz($seed);
}
$max = max($lengths);
echo("integer with longest sequence: " . ($start + array_search($max, $lengths)));
echo("<br>");
echo("length of longest sequence: " . $max);
?>