blob: 4303a961b36d5bec806f526c643432a4b951a3c4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php
function gcd($a, $b) {
// could of used https://www.php.net/manual/en/function.gmp-gcd.php
return ($a % $b) ? gcd($b,$a % $b) : $b;
}
function solutions($N) {
// $N can't be less than 2
if ($N <= 1) {
return false;
}
$total = 0;
for ($i = 1; $i <= $N; $i++) {
for ($next = $i + 1; $next <= $N; $next++) {
// debug purpose
printf ("gcd(%s, %s)\n", $i, $next);
$total += gcd($i, $next);
}
}
return $total;
}
echo solutions(3), "\n";
echo solutions(4), "\n";
|