Home > Article > Backend Development > Non-recursive method of processing Fibonacci sequence in PHP_PHP tutorial
I thought about it myself. In fact, the program to solve this problem is an offset problem. First look at the sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34. The next number in the sequence is the sum of the previous two numbers, and so on.
When processed by the program, it is actually a FOR statement. The traditional FOR statement is for($i=1;$i;$count,$i++), and the offset here is $i=$i+1. If If you process this array, the offset is not 1, but the previous number. Then when you do for, one variable records the previous number, and the other records the current number. The offset is the previous number, and then reassigns the value in the loop, recording the previous number as the natural loop value, and then does the following cycle offset. The code is actually very simple: