Fibonacci sequence Solution

Source: Internet
Author: User

Recently, when I looked at the data structure and algorithm, I saw a poor solution to the simple problem. I gave a PHP version of the Fibonacci series solution by referring to the network information.

1234567891011121314151617181920212223242526272829303132 /** * Description: Fibonacci sequence (subscript starts from 0) * Author: junin927@hotmail.com, Junin South-North * Author Page: http://www.j927.net * Publish: 2011-01-02 * Last Modify: 2011-01-02 * Remark. * * @param int $n * @ Return int-1: input error n (n> 0): sequence result */function
fibonacci($n) {
// Iterative Solution    if
($n<0)
return -1;    if
($n<=1)
return 1;    $f0
= $f1
= 1;
    for
($i=1;$i<$n;$i++)
{
        $f
= $f0+$f1;        $f0
= $f1;        $f1
= $f;    }    return
$f;}/* Function fibonacci ($ n) {// recursive Solution    if ($n<0) return -1;    if ($n<=1) return 1;    return fibonacci($n-1)+fibonacci($n-2);}*//* Function fibonacci ($ n) {// use the generic formula    if ($n<0) return -1;    $n++;    return (pow((1 + sqrt(5))/2,$n)-pow((1-sqrt(5))/2,$n))/sqrt(5);}*/
Data Structure & algorithm php,

Data structure,
Fibonacci, Algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.