-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem26.php
49 lines (41 loc) · 1.14 KB
/
Problem26.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
<?php
namespace ProjectEuler;
$problem = new Problem26();
$problem->handle();
class Problem26
{
public int $value = 0;
public int $maxCycle = 0;
function handle()
{
for ($i = 2; $i < 1000; $i++) {
$cycle = $this->getRecurringCycle($i);
if ($cycle) {
if ($cycle > $this->maxCycle) {
$this->maxCycle = $cycle;
$this->value = $i;
}
}
}
var_dump([
'value' => $this->value,
'maxCycle' => $this->maxCycle
]);
}
function getRecurringCycle($denominator): int
{
$numerator = 1;
$remainder_map = [];
$remainder = $numerator % $denominator;
while (!array_key_exists($remainder, $remainder_map) && $remainder != 0) {
$remainder_map[$remainder] = count($remainder_map);
$remainder *= 10;
$remainder %= $denominator;
}
if ($remainder == 0) {
return 0; // non-repeating decimal
} else {
return count($remainder_map) - $remainder_map[$remainder];
}
}
}