The question goes like this:
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By >starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
By considering the terms in the Fibonacci sequence whose values do not exceed four million, >find the sum of the even-valued terms.
And this is what I wrote in PHP
<?php
function fibo($first, $second, $limit){
$next = $first + $second;
if ($next % 2 ==0) {
$array[]= "$next";
}
do
{
fibo($second,$next, $limit);
} while ($next < $limit);
$sum=array_sum($array[]);
echo "$sum";
}
fibo(1,2,4000000);
?>
My code doesn’t run though…can anyone help?
Try changing
$sum=array_sum($array[]);to$sum=array_sum($array);and see what it happens…Also, when you have a problem, it’s good to let the others know what kind of problem it is. Like the error message or something like that…
If by any chance there are no errors reported, try to turn them on.
EDIT:
I remember that when solving some Euler problems recursively I happened to have a problem where the page crashed. This may happen due to some recursion limitations. I believe it’s similar to your problem.