示例#1
0
function sumAmicableNumbers($max)
{
    $sum = 0;
    for ($i = 1; $i < $max; ++$i) {
        if (isAmicable($i)) {
            $sum += $i;
        }
    }
    return $sum;
}
示例#2
0
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);