Home  >  Article  >  Backend Development  >  Non-recursive method of processing Fibonacci sequence in PHP_PHP tutorial

Non-recursive method of processing Fibonacci sequence in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:21:511138browse

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:

Copy code The code is as follows:

$count = 99999999999967543;
$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.'there'.count( $array).'Fibonacci Sequence Numbers';

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324859.htmlTechArticleI 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 first 2...
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