Recursive Method for the Fibonacci series (c)

Source: Internet
Author: User

Fibonacci series:

It is also called the golden split series, which refers to a series of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144 ,...

In mathematics, the Fibonacci series are defined as follows by recursive Methods: F0 = 0, F1 = 1, Fn = F (n-1) + F (n-2) (n> = 2, n, N *),

That is, this series starts from the second item, and each item is equal to the sum of the first two items.

Note: 0 is 0th items, not 1st items.

Use recursion to obtain the Fibonacci sequence and list all items:

# Include

 
  
Int fun (int n) // n indicates the number of items. Note: 0 is 0th items, not 1st items. {If (n <= 1) return n; else return fun (n-1) + fun (n-2);} int main () {int n; printf ("Enter the number of items to output (Natural Number) Fibonacci sequence:"); scanf ("% d", & n); // int * a = (int *) malloc (n + 1) * sizeof (int); // if you need to store, use dynamic memory to allocate n + 1 space for int I storage; for (I = 0; I <n + 1; I ++) // output all items {printf ("% d,", fun (I); if (I! = 0 & I % 5 = 0) // wrap each of the five items (the first line contains one more than 0th items) printf ("\ n ");} printf ("item % d is: % d \ n", n, fun (n); // return 0 ;}

 

For example, 30th items (0-30 items are printed by the way)


Related Article

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.