Variable-length Fibonacci series

Source: Internet
Author: User

Original topic:

Write a recursive program that extends the range of the Fibonacci sequence. The Fibonacci sequence is 1, 1, 2, 3, 5, 8, etc., where each element is the sum of the previous and the elements. For this problem, instead of only adding the last and the last of the nth element, which has the nth Elem  ent be the sum of the previous K elements.  For example, the 7th Fibonacci element summing the previous 3 elements would is 37.  The sequence is 1 1 2 4 7 13. Be sure to check your code by running it with a K value of 2 and comparing the value to the value you would obtai N with the regular Fibonacci sequence (they should be the same)

Probably the meaning is the deformation of the Fibonacci series: that is, a number in the sequence is no longer the first two number of combined with, but the number of former K plus and, but also to be implemented by recursion.

The topic feels more interesting, so excerpt here, casually look.

Private Static intFibintNintk) {        intTmp=0; if(n<k) {                        if(n==1| | n==2)                return1; Else if(n<1)                return0;  for(inti=1;i<=n;i++) {tmp= tmp +FIB (ni,k); }                                            }        Else{             for(inti=1;i<=k;i++) {tmp+=FIB (ni,k); }                    }        returntmp; }

Variable-length 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.