Related Articles

# N-bonacci Numbers

• Difficulty Level : Easy

You are given two integers N and M, and print all the terms of the series up to M-terms of the N-bonacci Numbers. For example, when N = 2, the sequence becomes Fibonacci, when n = 3, sequence becomes Tribonacci.

In general, in N-bonacci sequence, we use sum of preceding N numbers from the next term. For example, a 3-bonacci sequence is the following:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

The Fibonacci sequence is a set of numbers that starts with one or zero, followed by a one, and proceeds based on the rule that each number is equal to the sum of preceding two numbers 0, 1, 1, 2, 3, 5, 8…..

Examples :

```Input : N = 3, M = 8
Output : 0, 0, 1, 1, 2, 4, 7, 13
We need to print first M terms.
First three terms are 0, 0 and 1.
Fourth term is 0 + 0 + 1 = 1
Fifth term is 0 + 1 + 1 = 2
Sixth terms is 1 + 1 + 2 = 4
Seventh term is 7 (1 + 2 + 4) and eighth
term is 13 (7 + 4 + 2).

Input : N = 4, M = 10
Output : 0 0 0 1 1 2 4 8 15 29 ```

Method 1 (Simple)

Initialize first N-1 terms as 0 and N-th term as 1. Now to find terms from (N+1)-th to M-th, we simply compute sum of previous N terms.

Example : N = 4, M = 9

First three terms are 0, 0, 0

Fourth term is 1.

Remaining terms are computed by adding

previous 4 terms.
0 0 0 1
0 0 0 1 1
0 0 0 1 1 2
0 0 0 1 1 2 4
0 0 0 1 1 2 4 8
0 0 0 1 1 2 4 8 15
0 0 0 1 1 2 4 8 15 29

## C++

 `// CPP program print first M terms of``// N-bonacci series.``#include ``using` `namespace` `std;`` ` `// Function to print bonacci series``void` `bonacciseries(``long` `n, ``int` `m)``{``    ``// Assuming m >= n.``    ``int` `a[m] = { 0 };``    ``a[n - 1] = 1;`` ` `    ``// Computing every term as sum of previous``    ``// n terms.``    ``for` `(``int` `i = n; i < m; i++)``        ``for` `(``int` `j = i - n; j < i; j++)``            ``a[i] += a[j];`` ` `    ``for` `(``int` `i = 0; i < m; i++)``        ``cout << a[i] << ``"  "``;``}`` ` `// Driver's Code``int` `main()``{``    ``int` `N = 5, M = 15;``    ``bonacciseries(N, M);``    ``return` `0;``}`

## Java

 `// Java program print first M ``// terms of N-bonacci series.``import` `java.io.*;`` ` `class` `GFG``{``    ``// Function to print``    ``// bonacci series``    ``static` `void` `bonacciseries(``int` `n, ``                              ``int` `m)``    ``{``        ``// Assuming m >= n.``        ``int` `[]a = ``new` `int``[m];``        ``a[n - ``1``] = ``1``;``      ` `        ``// Computing every term as ``        ``// sum of previous n terms.``        ``for` `(``int` `i = n; i < m; i++)``            ``for` `(``int` `j = i - n; j < i; j++)``                ``a[i] += a[j];``      ` `        ``for` `(``int` `i = ``0``; i < m; i++)``            ``System.out.print(a[i] + ``" "``);``    ``}``      ` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `N = ``5``, M = ``15``;``        ``bonacciseries(N, M);``    ``}``}``  ` `// This code is contributed by ``// Manish Shaw(manishshaw1)`

## Python3

 `# Python program print first M ``# terms of N-bonacci series.`` ` `# Function to print bonacci series``def` `bonacciseries(n, m) :`` ` `    ``# Assuming m >= n.``    ``a ``=` `[``0``] ``*` `m``    ``a[n ``-` `1``] ``=` `1`` ` `    ``# Computing every term as ``    ``# sum of previous n terms.``    ``for` `i ``in` `range``(n, m) :``        ``for` `j ``in` `range``(i ``-` `n, i) :``            ``a[i] ``=` `a[i] ``+` `a[j]`` ` `    ``for` `i ``in` `range``(``0``, m) :``        ``print` `(a[i], end ``=` `" "``)`` ` `# Driver Code``N ``=` `5``M ``=` `15``bonacciseries(N, M)`` ` `# This code is contributed ``# by Manish Shaw(manishshaw1)`

## C#

 `// C# program print first M ``// terms of N-bonacci series.``using` `System;`` ` `class` `GFG``{``    ``// Function to print``    ``// bonacci series``    ``static` `void` `bonacciseries(``int` `n, ``                              ``int` `m)``    ``{``        ``// Assuming m >= n.``        ``int` `[]a = ``new` `int``[m];``        ``Array.Clear(a, 0, a.Length);``        ``a[n - 1] = 1;``     ` `        ``// Computing every term as ``        ``// sum of previous n terms.``        ``for` `(``int` `i = n; i < m; i++)``            ``for` `(``int` `j = i - n; j < i; j++)``                ``a[i] += a[j];``     ` `        ``for` `(``int` `i = 0; i < m; i++)``            ``Console.Write(a[i] + ``" "``);``    ``}``     ` `    ``// Driver Code``    ``static` `void` `Main()``    ``{``        ``int` `N = 5, M = 15;``        ``bonacciseries(N, M);``    ``}``}`` ` `// This code is contributed by ``// Manish Shaw(manishshaw1)`

## PHP

 `= n.``    ``\$a` `= ``array_fill``(0, ``\$m``, 0);``    ``\$a``[``\$n` `- 1] = 1;`` ` `    ``// Computing every term as ``    ``// sum of previous n terms.``    ``for` `(``\$i` `= ``\$n``; ``\$i` `< ``\$m``; ``\$i``++)``        ``for` `(``\$j` `= ``\$i` `- ``\$n``; ``             ``\$j` `< ``\$i``; ``\$j``++)``            ``\$a``[``\$i``] += ``\$a``[``\$j``];`` ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$m``; ``\$i``++)``        ``echo` `(``\$a``[``\$i``].``" "``);``}`` ` `// Driver Code``\$N` `= 5; ``\$M` `= 15;``bonacciseries(``\$N``, ``\$M``);`` ` `// This code is contributed ``// by Manish Shaw(manishshaw1)``?>`

## Javascript

 ``
Output :

`0  0  0  0  1  1  2  4  8  16  31  61  120  236  464`

Time Complexity : O(M * N)

Auxiliary Space : O(M)

Method 2 (Optimized)

We can optimize for large values of N. The idea is based on sliding window. The current term a[i] can be computed as a[i-1] + a[i-1] – a[i-n-1]

## C++

 `// CPP program print first M terms of``// N-bonacci series.``#include ``using` `namespace` `std;`` ` `// Function to print bonacci series``void` `bonacciseries(``long` `n, ``int` `m)``{`` ` `    ``// Assuming m > n.``    ``int` `a[m] = { 0 };``    ``a[n - 1] = 1;``    ``a[n] = 1;`` ` `    ``// Uses sliding window``    ``for` `(``int` `i = n + 1; i < m; i++)``        ``a[i] = 2 * a[i - 1] - a[i - n - 1];`` ` `    ``// Printing result``    ``for` `(``int` `i = 0; i < m; i++)``        ``cout << a[i] << ``" "``;``}`` ` `// Driver's Code``int` `main()``{``    ``int` `N = 5, M = 15;``    ``bonacciseries(N, M);``    ``return` `0;``}`

## Java

 `// Java program print first M terms of``// N-bonacci series.``class` `GFG {``     ` `    ``// Function to print bonacci series``    ``static` `void` `bonacciseries(``int` `n, ``int` `m)``    ``{``     ` `        ``// Assuming m > n.``        ``int` `a[] = ``new` `int``[m];``        ``for``(``int` `i = ``0``; i < m; i++)``            ``a[i] = ``0``;``             ` `        ``a[n - ``1``] = ``1``;``        ``a[n] = ``1``;``     ` `        ``// Uses sliding window``        ``for` `(``int` `i = n + ``1``; i < m; i++)``            ``a[i] = ``2` `* a[i - ``1``] - a[i - n - ``1``];``     ` `        ``// Printing result``        ``for` `(``int` `i = ``0``; i < m; i++)``            ``System.out.print(a[i] + ``" "``);``    ``}``     ` `    ``// Driver's Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `N = ``5``, M = ``15``;``        ``bonacciseries(N, M);``    ``}``}`` ` `// This code is contributed by JaideepPyne.`

## Python3

 `# Python3 program print first M terms of``# N-bonacci series.`` ` `# Function to print bonacci series``def` `bonacciseries(n, m):`` ` `    ``# Assuming m > n.``    ``a ``=` `[``0` `for` `i ``in` `range``(m)]``    ``a[n ``-` `1``] ``=` `1``    ``a[n] ``=` `1`` ` `    ``# Uses sliding window``    ``for` `i ``in` `range``(n ``+` `1``, m):``        ``a[i] ``=` `2` `*` `a[i ``-` `1``] ``-` `a[i ``-` `n ``-` `1``]`` ` `    ``# Printing result``    ``for` `i ``in` `range``(``0``, m):``        ``print``(a[i], end``=``" "``)`` ` ` ` `# Driver's Code``if` `__name__``=``=``'__main__'``:``    ``N, M ``=` `5``, ``15``    ``bonacciseries(N, M)`` ` `# This code is contributed by``# Sanjit_Prasad`

## C#

 `// Java program print ``// first M terms of``// N-bonacci series.``using` `System;`` ` `class` `GFG ``{``     ` `    ``// Function to print``    ``// bonacci series``    ``static` `void` `bonacciseries(``int` `n, ``                              ``int` `m)``    ``{``     ` `        ``// Assuming m > n.``        ``int` `[]a = ``new` `int``[m];``        ``for``(``int` `i = 0; i < m; i++)``            ``a[i] = 0;``             ` `        ``a[n - 1] = 1;``        ``a[n] = 1;``     ` `        ``// Uses sliding window``        ``for` `(``int` `i = n + 1; i < m; i++)``            ``a[i] = 2 * a[i - 1] - ``                       ``a[i - n - 1];``     ` `        ``// Printing result``        ``for` `(``int` `i = 0; i < m; i++)``            ``Console.Write(a[i] + ``" "``);``    ``}``     ` `    ``// Driver Code``    ``static` `void` `Main()``    ``{``        ``int` `N = 5, M = 15;``        ``bonacciseries(N, M);``    ``}``}`` ` `// This code is contributed by``// Manish Shaw(manishshaw1)`

## PHP

 ` n.``    ``\$a` `= ``array``();``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$m``; ``\$i``++)``        ``\$a``[``\$i``] = 0;``    ``\$a``[``\$n` `- 1] = 1;``    ``\$a``[``\$n``] = 1;`` ` `    ``// Uses sliding window``    ``for` `(``\$i` `= ``\$n` `+ 1; ``\$i` `< ``\$m``; ``\$i``++)``        ``\$a``[``\$i``] = 2 * ``\$a``[``\$i` `- 1] - ``                     ``\$a``[``\$i` `- ``\$n` `- 1];`` ` `    ``// Printing result``    ``for` `(``\$i` `= 0; ``\$i` `< ``\$m``; ``\$i``++)``        ``echo` `(``\$a``[``\$i``] . ``" "``);``}`` ` `// Driver Code``\$N` `= 5; ``\$M` `= 15;``bonacciseries(``\$N``, ``\$M``);`` ` `// This code is contributed by ``// Manish Shaw(manishshaw1)``?>`

## Javascript

 ``
Output:
`0 0 0 0 1 1 2 4 8 16 31 61 120 236 464`

Time Complexity: O(M)

Auxiliary Space: O(M)

My Personal Notes arrow_drop_up