N-bonacci Numbers
Last Updated :
29 Mar, 2024
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
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 = 10
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++
#include <bits/stdc++.h>
using namespace std;
void bonacciseries( long n, int m)
{
int a[m] = { 0 };
a[n - 1] = 1;
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] << " " ;
}
int main()
{
int N = 5, M = 15;
bonacciseries(N, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void bonacciseries( int n, int m)
{
int [] a = new int [m];
a[n - 1 ] = 1 ;
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] + " " );
}
public static void main(String args[])
{
int N = 5 , M = 15 ;
bonacciseries(N, M);
}
}
|
Python3
def bonacciseries(n, m):
a = [ 0 ] * m
a[n - 1 ] = 1
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 = " " )
N = 5
M = 15
bonacciseries(N, M)
|
C#
using System;
class GFG {
static void bonacciseries( int n, int m)
{
int [] a = new int [m];
Array.Clear(a, 0, a.Length);
a[n - 1] = 1;
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] + " " );
}
static void Main()
{
int N = 5, M = 15;
bonacciseries(N, M);
}
}
|
PHP
<?php
function bonacciseries( $n , $m )
{
$a = array_fill (0, $m , 0);
$a [ $n - 1] = 1;
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 ]. " " );
}
$N = 5; $M = 15;
bonacciseries( $N , $M );
?>
|
Javascript
<script>
function bonacciseries(n , m)
{
var a = Array(m).fill(0);
a[n - 1] = 1;
for (i = n; i < m; i++)
for (j = i - n; j < i; j++)
a[i] += a[j];
for (i = 0; i < m; i++)
document.write(a[i] + " " );
}
var N = 5, M = 15;
bonacciseries(N, M);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
void bonacciseries( long n, int m)
{
int a[m] = { 0 };
a[n - 1] = 1;
a[n] = 1;
for ( int i = n + 1; i < m; i++)
a[i] = 2 * a[i - 1] - a[i - n - 1];
for ( int i = 0; i < m; i++)
cout << a[i] << " " ;
}
int main()
{
int N = 5, M = 15;
bonacciseries(N, M);
return 0;
}
|
Java
class GFG {
static void bonacciseries( int n, int m)
{
int a[] = new int [m];
for ( int i = 0 ; i < m; i++)
a[i] = 0 ;
a[n - 1 ] = 1 ;
a[n] = 1 ;
for ( int i = n + 1 ; i < m; i++)
a[i] = 2 * a[i - 1 ] - a[i - n - 1 ];
for ( int i = 0 ; i < m; i++)
System.out.print(a[i] + " " );
}
public static void main(String args[])
{
int N = 5 , M = 15 ;
bonacciseries(N, M);
}
}
|
Python3
def bonacciseries(n, m):
a = [ 0 for i in range (m)]
a[n - 1 ] = 1
a[n] = 1
for i in range (n + 1 , m):
a[i] = 2 * a[i - 1 ] - a[i - n - 1 ]
for i in range ( 0 , m):
print (a[i], end = " " )
if __name__ = = '__main__' :
N, M = 5 , 15
bonacciseries(N, M)
|
C#
using System;
class GFG {
static void bonacciseries( int n, int m)
{
int [] a = new int [m];
for ( int i = 0; i < m; i++)
a[i] = 0;
a[n - 1] = 1;
a[n] = 1;
for ( int i = n + 1; i < m; i++)
a[i] = 2 * a[i - 1] - a[i - n - 1];
for ( int i = 0; i < m; i++)
Console.Write(a[i] + " " );
}
static void Main()
{
int N = 5, M = 15;
bonacciseries(N, M);
}
}
|
PHP
<?php
function bonacciseries( $n , $m )
{
$a = array ();
for ( $i = 0; $i < $m ; $i ++)
$a [ $i ] = 0;
$a [ $n - 1] = 1;
$a [ $n ] = 1;
for ( $i = $n + 1; $i < $m ; $i ++)
$a [ $i ] = 2 * $a [ $i - 1] -
$a [ $i - $n - 1];
for ( $i = 0; $i < $m ; $i ++)
echo ( $a [ $i ] . " " );
}
$N = 5; $M = 15;
bonacciseries( $N , $M );
?>
|
Javascript
<script>
function bonacciseries(n , m)
{
var a = Array(m).fill(0);
for (i = 0; i < m; i++)
a[i] = 0;
a[n - 1] = 1;
a[n] = 1;
for (i = n + 1; i < m; i++)
a[i] = 2 * a[i - 1] - a[i - n - 1];
for (i = 0; i < m; i++)
document.write(a[i] + " " );
}
var N = 5, M = 15;
bonacciseries(N, M);
</script>
|
Output:
0 0 0 0 1 1 2 4 8 16 31 61 120 236 464
Time Complexity: O(M)
Auxiliary Space: O(M)
Method 3: Using Queue
We can use queue to easily and efficiently calculate every terms of N-bonacci numbers by using less space than above method.
Algorithm:
- First we push N element inside the queue whose values will be ‘0‘ for 1 to N-1 elements and ‘1‘ for Nth element.
- Now, we take another variable (let’s say su) to calculate N-bonacci numbers. and set its value to 1, which is the sum of all the elements, currently inside the queue.
- Then, using a for loop iterate the queue M times:
- Inside for loop, we pop the front element of the queue and store it.
- Then, we push an element whose value will be sum of all the elements, currently inside the queue.
- Push su into the queue.
- Before popping, the sum of all the elements is su, and then we push su inside queue.
- So, now the sum is 2*su.
- And, then we pop front element of the queue, so sum will be (2*su – popped_front_element).
- So, su will now become (2*su – popped_front_element), which we will be pushed inside the queue in next iteration.
- And then we print the popped element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void bonacciseries( int n, long m)
{
queue< int > q;
for ( int i = 0; i < (n - 1); i++) {
q.push(0);
}
q.push(1);
long su = 1;
for ( int i = 0; i < m; i++) {
q.push(su);
long temp = q.front();
q.pop();
su = (2 * su) - temp;
cout << temp << " " ;
}
}
int main()
{
int N = 5, M = 15;
bonacciseries(N, M);
return 0;
}
|
Java
import java.util.*;
public class bonacciSeries {
static void bonacciseries( int n, long m)
{
Queue<Integer> q = new LinkedList<>();
for ( int i = 0 ; i < (n - 1 ); i++) {
q.add( 0 );
}
q.add( 1 );
long su = 1 ;
for ( int i = 0 ; i < m; i++) {
q.add(( int )su);
long temp = q.peek();
q.remove();
su = ( 2 * su) - temp;
System.out.print(temp + " " );
}
}
public static void main(String[] args)
{
int N = 5 , M = 15 ;
bonacciseries(N, M);
}
}
|
Python3
from queue import Queue
def bonacciseries(n, m):
q = Queue()
for i in range (n - 1 ):
q.put( 0 )
q.put( 1 )
su = 1
for i in range (m):
q.put(su)
temp = q.queue[ 0 ]
q.get()
su = ( 2 * su) - temp
print (temp, end = " " )
if __name__ = = "__main__" :
N, M = 5 , 15
bonacciseries(N, M)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void bonacciseries( int n, long m)
{
Queue< int > q = new Queue< int >();
for ( int i = 0; i < (n - 1); i++) {
q.Enqueue(0);
}
q.Enqueue(1);
long su = 1;
for ( int i = 0; i < m; i++) {
q.Enqueue(( int )su);
int temp = q.Peek();
q.Dequeue();
su = (2 * su) - temp;
Console.Write(temp + " " );
}
}
static void Main()
{
int N = 5, M = 15;
bonacciseries(N, M);
}
}
|
Javascript
function bonacciseries(n, m) {
var q = [];
for (let i = 0; i < (n - 1); i++) {
q.push(0);
}
q.push(1);
let su = 1;
for (let i = 0; i < m; i++) {
q.push(su);
let temp = q.shift();
su = (2 * su) - temp;
console.log(temp + " " );
}
}
let N = 5, M = 15;
bonacciseries(N, M);
|
Output
0 0 0 0 1 1 2 4 8 16 31 61 120 236 464
Time Complexity: O(M), To iterate M times.
Auxiliary Space: O(N), because at every time queue has N elements.
Share your thoughts in the comments
Please Login to comment...