Fibonacci Sequence __ Algorithm

Source: Internet
Author: User

In the course of our learning algorithm, the Fibonacci sequence is definitely a thing to encounter, in fact, we are not in order to learn a simple sequence, more importantly, to learn his thoughts-recursion. I think recursion is the most high-frequency way to solve many algorithmic problems. The simplest description of recursion is that a function calls itself, and when it reaches a condition, it ends recursively. For example, we often use recursion in the following scenarios: Quick sort, merge sort, multiple traversal methods for binary trees, and so on. The code for the C-language implementation of the Fibonacci sequence has been uploaded to Https://github.com/chenyufeng1991/Fibonacci.

The recursive functions of the Fibonacci sequence are as follows:


Print each of the following:011235813...


The C language is implemented as follows:

C Language Implementation Fibonacci series
//0 1 1 2 3 5 8
#include <stdio.h>
#include <stdlib.h>

int Fibonacci (int num);

int main (int argc, const char * argv[])
{
    int index = FIBONACCI (6);
    printf ("%d", index);
    return 0;
}

int Fibonacci (int num)
{
    //recursive end condition
    if (num = 0)
    {return
        0;
    }

    if (num = = 1)
    {return
        1;
    }

    Return Fibonacci (num-1) + Fibonacci (num-2);
}


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.