Use recursion to calculate the Fibonacci sequence value of a specified number of digits

Source: Internet
Author: User

Yesterday, I encountered a question like 1, 1, 2, 3, 5, 8, 13, 21.... What is the value of 30th?CodeThe implementation is as follows:

 1       //  30th, 13, 21 ...... what is?  2       //  Use recursion to calculate the Fibonacci sequence value of a specified number of digits  3       //  Fn = f (n-1) + f (n-2)  4       Public   Static   Int Getfibonaccinumber ( Int Index)  5   {  6           If (Index < 0 | Index = 0 ) Throw   New Exception ( "  The parameter cannot be less than or equal to 0.  "  );  7           If (Index <= 2 )  8   {  9               Return   1  ;  10   }  11           Else  12   {  13               Return Getfibonaccinumber (index- 1 ) + Getfibonaccinumber (index-2  );  14   }  15 }

 

Additional reading

Wikipedia: Fibonacci Series

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.