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

Non-recursive method of processing Fibonacci sequence in PHP_PHP tutorial

WBOY
Release: 2016-07-21 15:21:51
Original
1154 people have browsed it

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template