Home > Backend Development > PHP Tutorial > How to implement Fibonacci sequence in PHP? (code example)

How to implement Fibonacci sequence in PHP? (code example)

藏色散人
Release: 2023-04-05 16:34:01
Original
8259 people have browsed it

Fibonacci sequence, also known as the golden section sequence, was introduced by the mathematician Leonardoda Fibonacci using rabbit reproduction as an example, so it is also called the "rabbit sequence" ", refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34,..., simply put, the Fibonacci sequence is a series of elements, the first two elements are added together Get the next element, starting from 0 and 1.

How to implement Fibonacci sequence in PHP? (code example)

In this article, we will introduce to youHow to use PHP to implement the Fibonacci sequence. Given a number n, we need to find the Fibonacci sequence up to the nth term.

Example:

输入:10 
输出:0 1 1 2 3 5 8 13 21 34 
输入:15 
输出:0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Copy after login

Method 1: Using recursion

Recursion is how we call the same function repeatedly until the basic condition is matched to end the recursion.

<?php
function Fibonacci($number){

    if ($number == 0)
        return 0;
    else if ($number == 1)
        return 1;
    else
        return (Fibonacci($number-1) +
            Fibonacci($number-2));
}
$number = 10;
for ($counter = 0; $counter < $number; $counter++){
    echo Fibonacci($counter),&#39; &#39;;
}
Copy after login

Output:

0 1 1 2 3 5 8 13 21 34
Copy after login
Copy after login

Method 2: Using iterative method

First, we initialize the first and second numbers to 0 and 1. Then, we print the first and second numbers. Then we send the process to the iterative while loop where we get the next number by adding the previous two numbers, at the same time we swap the first number with the second number and the second number with the third number.

<?php
function Fibonacci($n){

    $num1 = 0;
    $num2 = 1;

    $counter = 0;
    while ($counter < $n){
        echo &#39; &#39;.$num1;
        $num3 = $num2 + $num1;
        $num1 = $num2;
        $num2 = $num3;
        $counter = $counter + 1;
    }
}

$n = 10;
Fibonacci($n);
Copy after login

Output:

0 1 1 2 3 5 8 13 21 34
Copy after login
Copy after login

Related video tutorial recommendation: "PHP Tutorial"

This article is about implementing Fibonacci in PHP An introduction to the method of sequence of numbers, I hope it will be helpful to friends who need it!

The above is the detailed content of How to implement Fibonacci sequence in PHP? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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