Home>Article>Backend Development> PHP implements Fibonacci sequence method
This article mainly introduces the code sharing of Fibonacci sequence in PHP. It has certain reference value. Friends in need can refer to it. I hope it can help everyone.
The Fibonacci sequence refers to a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597 , 2584, 4181, 6765, 10946, 17711, 28657, 46368...
This sequence starts from the 3rd item, and each item is equal to the sum of the previous two items.
F0=0, F1=1, Fn=F(n-1)+F(n-2)
Recursive version and non-recursive version.
As a C and Java programmer, the first time I wrote non-recursive code, I forgot to add $ before the variable. , sad.
Output result
Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 8 [6] => 13 [7] => 21 [8] => 34 [9] => 55 ) ------------------ 55
Have you learned how to strike? Hurry up and give it a try.
Related recommendations:
Detailed explanation of python output Fibonacci sequence
js implementation of Fibonacci sequence
Recursion and recursion to implement Fibonacci sequence algorithm
The above is the detailed content of PHP implements Fibonacci sequence method. For more information, please follow other related articles on the PHP Chinese website!