Ejemplo n.º 1
0
</title>

<body>

<?php 
function find_divisors($number)
{
    $divisors = array();
    $number_sqrt = sqrt($number);
    for ($i = 1; $i <= $number_sqrt; $i++) {
        if ($number % $i == 0) {
            array_push($divisors, $i, $number / $i);
        }
    }
    return $divisors;
}
function print_array($array)
{
    for ($i = 0; $i < count($array) - 1; $i++) {
        // you can use count or sizeof($array)
        echo "{$array[$i]},";
    }
    echo $array[count($array) - 1];
}
$divs = find_divisors(720);
print_array($divs);
?>

</body>

</html>
Ejemplo n.º 2
0
<?php

$house = 1;
$max = 29000000 / 10;
function find_divisors($num)
{
    $max = $num / 2;
    // twice faster
    $divisors = array($num);
    // but we have to add one manually
    for ($i = 1; $i <= $max; $i++) {
        if ($num % $i == 0) {
            $divisors[] = $i;
        }
    }
    return $divisors;
}
$time_start = microtime(true);
do {
    $current = array_sum(find_divisors($house));
    //var_dump($current);print "<br/>";
    $house++;
} while ($current <= $max);
print "<br/><br/>time: ";
$time_end = microtime(true);
print $time_end - $time_start . "<br/><br/>";
var_dump($current);
var_dump($house);