Home  >  Article  >  Backend Development  >  How to implement Fibonacci sequence in php

How to implement Fibonacci sequence in php

不言
不言Original
2018-04-18 10:02:392262browse

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;

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!

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