Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
n_bonacci.cpp File Reference

Implementation of the N-bonacci series. More...

#include <cassert>
#include <iostream>
#include <vector>
Include dependency graph for n_bonacci.cpp:

Namespaces

namespace  math
 for IO operations
 
namespace  n_bonacci
 Functions for the N-bonacci implementation.
 

Functions

std::vector< uint64_t > math::n_bonacci::N_bonacci (const uint64_t &n, const uint64_t &m)
 Finds the N-Bonacci series for the n parameter value and m parameter terms.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation of the N-bonacci series.

In general, in N-bonacci sequence, we generate 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 In this code we take N and M as input where M is the number of terms to be printed of the N-bonacci series

Author
Swastika Gupta

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
104 {
105 test(); // run self-test implementations
106 return 0;
107}
static void test()
Self-test implementations.
Definition n_bonacci.cpp:69
Here is the call graph for this function:

◆ N_bonacci()

std::vector< uint64_t > math::n_bonacci::N_bonacci ( const uint64_t & n,
const uint64_t & m )

Finds the N-Bonacci series for the n parameter value and m parameter terms.

Parameters
nis in the N-Bonacci series
mis the number of terms in the N-Bonacci sequence
Returns
the n-bonacci sequence as vector array

we initialise the (n-1)th term as 1 which is the sum of preceding N zeros

similarily the sum of preceding N zeros and the (N+1)th 1 is also 1

40 {
42 m, 0); // we create an array of size m filled with zeros
43 if (m < n || n == 0) {
44 return a;
45 }
46
47 a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of
48 /// preceding N zeros
49 if (n == m) {
50 return a;
51 }
52 a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is
53 /// also 1
54 for (uint64_t i = n + 1; i < m; i++) {
55 // this is an optimized solution that works in O(M) time and takes O(M)
56 // extra space here we use the concept of the sliding window the current
57 // term can be computed using the given formula
58 a[i] = 2 * a[i - 1] - a[i - 1 - n];
59 }
60 return a;
61}
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
69 {
70 struct TestCase {
71 const uint64_t n;
72 const uint64_t m;
73 const std::vector<uint64_t> expected;
74 TestCase(const uint64_t in_n, const uint64_t in_m,
76 : n(in_n), m(in_m), expected(data) {
77 assert(data.size() == m);
78 }
79 };
80 const std::vector<TestCase> test_cases = {
81 TestCase(0, 0, {}),
82 TestCase(0, 1, {0}),
83 TestCase(0, 2, {0, 0}),
84 TestCase(1, 0, {}),
85 TestCase(1, 1, {1}),
86 TestCase(1, 2, {1, 1}),
87 TestCase(1, 3, {1, 1, 1}),
88 TestCase(5, 15, {0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464}),
90 6, 17,
91 {0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976}),
92 TestCase(56, 15, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})};
93
94 for (const auto &tc : test_cases) {
95 assert(math::n_bonacci::N_bonacci(tc.n, tc.m) == tc.expected);
96 }
97 std::cout << "passed" << std::endl;
98}
T endl(T... args)
int data[MAX]
test data
Definition hash_search.cpp:24
represents single example inputs and expected output of the function longest_common_string_length
Definition longest_common_string.cpp:54
Here is the call graph for this function: