<?php
function fibonacci($n){
if($n==1 ||$n==2){
return 1;
}else{
return fibonacci($n-1)+fibonacci($n-2);
}
}
for($x=1;$x<=10;$x++){
if ($x==1){
echo '0,';
}
if ($x!=10){
echo fibonacci($x).',';
} else {
echo fibonacci($x);
}
}
?>
风豆丁2017-08-23 22:44:31
This is called recursion, not callback
Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13....
The first number, the second value is 1, this is a rule of.
Starting from the third number, the value of the current number is the sum of the previous two numbers. This is the inherent law of the Fibonacci sequence.
Use recursive thinking to find the value of the nth number: fibonacci($n) = fibonacci($n-1)+fibonacci($n-2);
HUNT2017-08-21 06:45:50
The picture is based on my understanding, I hope it can help you understand better what my problem is