1
1
/* *
2
2
* @file
3
- * @brief Implementation of the [N-bonacci](http://oeis.org/wiki/N-bonacci_numbers) series
3
+ * @brief Implementation of the
4
+ * [N-bonacci](http://oeis.org/wiki/N-bonacci_numbers) series
4
5
*
5
6
* @details
6
7
* In general, in N-bonacci sequence,
@@ -30,18 +31,22 @@ namespace math {
30
31
*/
31
32
namespace n_bonacci {
32
33
/* *
33
- * @brief Finds the N-Bonacci series for the `n` parameter value and `m` parameter terms
34
+ * @brief Finds the N-Bonacci series for the `n` parameter value and `m`
35
+ * parameter terms
34
36
* @param n is in the N-Bonacci series
35
37
* @param m is the number of terms in the N-Bonacci sequence
36
38
* @returns the n-bonacci sequence as vector array
37
39
*/
38
40
std::vector<int > N_bonacci (int n, unsigned int m) {
39
41
std::vector<int > a (m, 0 ); // we create an empty array of size m
40
42
41
- a[n - 1 ] = 1 ; // / we initialise the (n-1)th term as 1 which is the sum of preceding N zeros
42
- a[n] = 1 ; // / similarily the sum of preceding N zeros and the (N+1)th 1 is also 1
43
+ a[n - 1 ] = 1 ; // / we initialise the (n-1)th term as 1 which is the sum of
44
+ // / preceding N zeros
45
+ a[n] = 1 ; // / similarily the sum of preceding N zeros and the (N+1)th 1 is
46
+ // / also 1
43
47
for (int i = n + 1 ; i < m; i++) {
44
- // this is an optimized solution that works in O(M) time and takes O(M) extra space here we use the concept of the sliding window the current
48
+ // this is an optimized solution that works in O(M) time and takes O(M)
49
+ // extra space here we use the concept of the sliding window the current
45
50
// term can be computed using the given formula
46
51
a[i] = 2 * a[i - 1 ] - a[i - 1 - n];
47
52
}
@@ -57,32 +62,51 @@ std::vector<int> N_bonacci(int n, unsigned int m) {
57
62
static void test () {
58
63
// n = 1 m = 1 return [1, 1]
59
64
std::cout << " 1st test" ;
60
- std::vector<int > arr1 = math::n_bonacci::N_bonacci (1 , 1 ); // first input is the param n and second one is the param m for N-bonacci func
61
- std::vector<int > output_array1 = {1 , 1 }; // It is the expected output series of length m
65
+ std::vector<int > arr1 = math::n_bonacci::N_bonacci (
66
+ 1 , 1 ); // first input is the param n and second one is the param m for
67
+ // N-bonacci func
68
+ std::vector<int > output_array1 = {
69
+ 1 , 1 }; // It is the expected output series of length m
62
70
assert (std::equal (std::begin (arr1), std::end (arr1),
63
71
std::begin (output_array1)));
64
72
std::cout << " passed" << std::endl;
65
73
66
- // n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464]
74
+ // n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236,
75
+ // 464]
67
76
std::cout << " 2nd test" ;
68
- std::vector<int > arr2 = math::n_bonacci::N_bonacci (5 , 15 ); // first input is the param n and second one is the param m for N-bonacci func
69
- std::vector<int > output_array2 = {0 , 0 , 0 , 0 , 1 , 1 , 2 , 4 , 8 , 16 , 31 , 61 , 120 , 236 , 464 }; // It is the expected output series of length m
77
+ std::vector<int > arr2 = math::n_bonacci::N_bonacci (
78
+ 5 , 15 ); // first input is the param n and second one is the param m for
79
+ // N-bonacci func
80
+ std::vector<int > output_array2 = {
81
+ 0 , 0 , 0 , 0 , 1 , 1 , 2 , 4 ,
82
+ 8 , 16 , 31 , 61 , 120 , 236 , 464 }; // It is the expected output series of
83
+ // length m
70
84
assert (std::equal (std::begin (arr2), std::end (arr2),
71
85
std::begin (output_array2)));
72
86
std::cout << " passed" << std::endl;
73
87
74
- // n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976]
88
+ // n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248,
89
+ // 492, 976]
75
90
std::cout << " 3rd test" ;
76
- std::vector<int > arr3 = math::n_bonacci::N_bonacci (6 , 17 ); // first input is the param n and second one is the param m for N-bonacci func
77
- std::vector<int > output_array3 = {0 , 0 , 0 , 0 , 0 , 1 , 1 , 2 , 4 , 8 , 16 , 32 , 63 , 125 , 248 , 492 , 976 }; // It is the expected output series of length m
91
+ std::vector<int > arr3 = math::n_bonacci::N_bonacci (
92
+ 6 , 17 ); // first input is the param n and second one is the param m for
93
+ // N-bonacci func
94
+ std::vector<int > output_array3 = {
95
+ 0 , 0 , 0 , 0 , 0 , 1 , 1 , 2 , 4 ,
96
+ 8 , 16 , 32 , 63 , 125 , 248 , 492 , 976 }; // It is the expected output series
97
+ // of length m
78
98
assert (std::equal (std::begin (arr3), std::end (arr3),
79
99
std::begin (output_array3)));
80
100
std::cout << " passed" << std::endl;
81
101
82
102
// n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
83
103
std::cout << " 4th test" ;
84
- std::vector<int > arr4 = math::n_bonacci::N_bonacci (56 , 15 ); // first input is the param n and second one is the param m for N-bonacci func
85
- std::vector<int > output_array4 = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }; // It is the expected output series of length m
104
+ std::vector<int > arr4 = math::n_bonacci::N_bonacci (
105
+ 56 , 15 ); // first input is the param n and second one is the param m
106
+ // for N-bonacci func
107
+ std::vector<int > output_array4 = {
108
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
109
+ 0 , 0 , 0 , 0 , 0 , 0 , 0 }; // It is the expected output series of length m
86
110
assert (std::equal (std::begin (arr4), std::end (arr4),
87
111
std::begin (output_array4)));
88
112
std::cout << " passed" << std::endl;
0 commit comments