Home > Backend Development > PHP Problem > How to implement Fibonacci sequence using php

How to implement Fibonacci sequence using php

青灯夜游
Release: 2023-03-17 22:24:01
Original
4678 people have browsed it

Implementation method: 1. Use array to find, syntax "for($i=0;$i

How to implement Fibonacci sequence using php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Fibonacci numbers are What

Fibonacci sequence, also known as the golden section sequence, because the mathematician Leonardoda Fibonacci used rabbit reproduction as an example It is introduced, so it is also called the "rabbit sequence", which refers to such a sequence:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765.......
Copy after login

That is: the first two values ​​​​are both 1, starting from the third digit, each digit is the digit before the current digit. The sum of two digits

How to implement Fibonacci sequence using php

In mathematics, the Fibonacci sequence is defined recursively as follows:

F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)
Copy after login
  • F: refers to the current sequence

  • n: the subscript of the exponential column

Okay, I understand Fiji Bonacci Sequence, let’s take a look at how to implement it using PHP.

Method 1: Use arrays

Observe the sequence given above and combine it with array knowledge to analyze:

  • When the array subscript is 0 or 1, the value of the element is 1;

  • When the array subscript is 2, the element value It is a[0] a[1];

  • . When the array index is 3, the element is a[1] a[2];

  • ....

  • When the array index is n, the element is a[n-2] a [n-1];

can be concluded:

  • ##a[0]=1

  • a[1]=2

  • ##a[n]=a[n-2] a [n-1]

    (n>2)

  • Code implementation:
<?php
header("Content-type:text/html;charset=utf-8");
function test($num){
    $arr=[];
    for($i=0;$i<$num;$i++)
    {
        if($i==0 || $i==1){
            $arr[$i]=1;
        }else{
            $arr[$i]=$arr[$i-1]+$arr[$i-2];
        }
		echo $arr[$i]." ";
    }
}
echo "斐波那契数列前10位:";
test(10);
echo "<br>斐波那契数列前11位:";
test(11);
echo "<br>斐波那契数列前12位:";
test(12);
?>
Copy after login

Output:

## Now that we understand how to use arrays to find the Fibonacci sequence, let's take a look at using recursion to find the Fibonacci sequence. How to implement Fibonacci sequence using php

Method 2: Using recursion

<?php
header("Content-type:text/html;charset=utf-8");
function fbnq($n) {
	if ($n <= 0) {
		return 0;
	}
	if ($n == 1 || $n == 2) {
		return 1;
	}
	return fbnq($n - 1) + fbnq($n - 2);
}

echo "斐波那契数列第10位:" . fbnq(10);
echo "<br>斐波那契数列第11位:" . fbnq(11);
echo "<br>斐波那契数列第12位:" . fbnq(12);
?>
Copy after login
Output:


The recursive method has also been implemented, isn’t it very simple! How to implement Fibonacci sequence using php

The recursive algorithm can solve a responsible problem using shorter code, but its operation efficiency is relatively low.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to implement Fibonacci sequence using php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template