示例#1
0
function fibonacci_rec($n)
{
    if ($n == 1 or $n == 2) {
        $term = 1;
    } else {
        if ($n > 0) {
            $term = fibonacci_rec($n - 1) + fibonacci_rec($n - 2);
        } else {
            $term = fibonacci_rec($n + 2) - fibonacci_rec($n + 1);
        }
    }
    return $term;
}
示例#2
0
 source: https://projecteuler.net/problem=2 <br>
XXX;
//Title
echo $problem;
echo "<br>";
//Method 1 - Recursion
start_method();
$n = 1;
$sum = 0;
$fibn = fibonacci_rec($n);
do {
    if (is_even($fibn)) {
        $sum += $fibn;
    }
    $n++;
    $fibn = fibonacci_rec($n);
} while ($fibn < $below);
end_method();
result("recursive fibonaccy function", "sum of fibonaccy even terms", $sum, "1", $stime);
//Method 2 - Calc n fibonacci
start_method();
$n = 1;
$sum = 0;
$fibn = fibonacci_calc($n);
do {
    if (is_even($fibn)) {
        $sum += $fibn;
    }
    $n++;
    $fibn = fibonacci_calc($n);
} while ($fibn < $below);