search
HomeBackend DevelopmentPHP ProblemHow to do Fibonacci sequence in php

How to do Fibonacci sequence in php

Sep 27, 2019 pm 04:42 PM
phpFibonacci Sequence

How to do Fibonacci sequence in php

Fibonacci sequence, also known as the golden section sequence, was named after 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,..., simply put, the Fibonacci Sequence is Series elements, the first two elements are added to get the next element, starting from 0 and 1.

How to implement Fibonacci sequence in PHP?

In this article, we will introduce to you how to implement the Fibonacci sequence with PHP. 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

Related recommendations: "php tutorial"

Method 1: Use recursion

Recursion is how we call the same function repeatedly until a 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;;
}

Output:

0 1 1 2 3 5 8 13 21 34

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);

Output:

0 1 1 2 3 5 8 13 21 34

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.