beyondgrader.com Logo
DemoBrowseAboutTeamLogin

N-bonacci

[email protected] // 1.0

The classic Fibonacci sequence starts with 0 and 1, after which each term is the sum of the previous two. Let's generalize this to the N-bonacci sequence, where the first N terms are 0, 1, …, N - 1 and each later term is the sum of the N previous terms.

Write a function called nbonacci which takes two int parameters, N and the zero-based term index. N will always be positive and the term index will always be nonnegative.

For example, nbonacci(2, 4) should return 3 because the 2-bonacci sequence (which is the classic Fibonacci sequence) begins 0, 1, (0 + 1) = 1, (1 + 1) = 2, (1 + 2) = 3. nbonacci(3, 5) should return 11 because the 3-bonacci sequence begins 0, 1, 2, (0 + 1 + 2) = 3, (1 + 2 + 3) = 6, (2 + 3 + 6) = 11.