function sumAmicableNumbers($max) { $sum = 0; for ($i = 1; $i < $max; ++$i) { if (isAmicable($i)) { $sum += $i; } } return $sum; }
include "helper.php"; function sumFactors($num) { $factors = array(1); for ($i = $num - 1; $i > 1; $i--) { if ($num % $i == 0) { $factors[] = $i; } } return array_sum($factors); } function isAmicable($a, $b) { if (sumFactors($b) == $a && $a != $b) { return true; } else { return false; } } $amicable = array(); for ($a = 1; $a <= 10000; $a++) { if (!in_array($a, $amicable)) { $b = sumFactors($a); if (isAmicable($a, $b)) { $amicable[] = $a; $amicable[] = $b; } } } $total = array_sum($amicable); result(31626, $total);