Home >Backend Development >PHP Tutorial >Fibonacci Sequence PHP non-recursive method of processing Fibonacci Sequence

Fibonacci Sequence PHP non-recursive method of processing Fibonacci Sequence

WBOY
WBOYOriginal
2016-07-29 08:47:561175browse

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.
If 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 you process this In the case of a sequence, 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:

Copy the code The code is as follows:


$count = 9999999999967543;
$array = array('0′=>1);
for($a=1,$i= 2;$i<$count;$i=$i+$a){
$array[] = $a;
$array[] = $i;
$a = $a +$i;
}
print_r( $array);
echo $count.' contains '.count($array).' Fibonacci sequence numbers';


I suggest any boring person take this to phpchina and post it to Dabaicai Career

The above introduces the non-recursive method of processing Fibonacci sequence in PHP, including the content of Fibonacci sequence. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn