Home > Backend Development > PHP Tutorial > How to implement Fibonacci sequence in php

How to implement Fibonacci sequence in php

不言
Release: 2023-03-24 09:30:01
Original
2454 people have browsed it

The content of this article is about the implementation of Fibonacci sequence in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it

一:function one($n){  
    $array = array();  
    $array[0] = 1;  
    $array[1] = 1;  
    for($i=2;$i<$n;$i++){  
        $array[$i] = $array[$i-1]+$array[$i-2];  
    }  
    print_r($array);  
}  
one(10);  




echo "\n------------------\n"; 

二:function two($n){  
    if($n==1||$n==2){return 1;}  
    else{  
        return two($n-1)+two($n-2);  
    }  
}  
echo two(10);die;
Copy after login

Related recommendations:

PHP implements KMP algorithm

PHP implements the recursive algorithm and the iterative algorithm of the Tower of Hanoi problem

The above is the detailed content of How to implement Fibonacci sequence in php. 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