|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation of the |
| 4 | + * [N-bonacci](http://oeis.org/wiki/N-bonacci_numbers) series |
| 5 | + * |
| 6 | + * @details |
| 7 | + * In general, in N-bonacci sequence, |
| 8 | + * we generate sum of preceding N numbers from the next term. |
| 9 | + * |
| 10 | + * For example, a 3-bonacci sequence is the following: |
| 11 | + * 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 |
| 12 | + * In this code we take N and M as input where M is the number of terms |
| 13 | + * to be printed of the N-bonacci series |
| 14 | + * |
| 15 | + * @author [Swastika Gupta](https://github.com/Swastyy) |
| 16 | + */ |
| 17 | + |
| 18 | +#include <algorithm> /// for std::is_equal, std::swap |
| 19 | +#include <cassert> /// for assert |
| 20 | +#include <iostream> /// for IO operations |
| 21 | +#include <vector> /// for std::vector |
| 22 | + |
| 23 | +/** |
| 24 | + * @namespace math |
| 25 | + * @brief Mathematical algorithms |
| 26 | + */ |
| 27 | +namespace math { |
| 28 | +/** |
| 29 | + * @namespace n_bonacci |
| 30 | + * @brief Functions for the [N-bonacci](http://oeis.org/wiki/N-bonacci_numbers) |
| 31 | + * implementation |
| 32 | + */ |
| 33 | +namespace n_bonacci { |
| 34 | +/** |
| 35 | + * @brief Finds the N-Bonacci series for the `n` parameter value and `m` |
| 36 | + * parameter terms |
| 37 | + * @param n is in the N-Bonacci series |
| 38 | + * @param m is the number of terms in the N-Bonacci sequence |
| 39 | + * @returns the n-bonacci sequence as vector array |
| 40 | + */ |
| 41 | +std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) { |
| 42 | + std::vector<uint64_t> a(m, 0); // we create an empty array of size m |
| 43 | + |
| 44 | + a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of |
| 45 | + /// preceding N zeros |
| 46 | + a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is |
| 47 | + /// also 1 |
| 48 | + for (uint64_t i = n + 1; i < m; i++) { |
| 49 | + // this is an optimized solution that works in O(M) time and takes O(M) |
| 50 | + // extra space here we use the concept of the sliding window the current |
| 51 | + // term can be computed using the given formula |
| 52 | + a[i] = 2 * a[i - 1] - a[i - 1 - n]; |
| 53 | + } |
| 54 | + return a; |
| 55 | +} |
| 56 | +} // namespace n_bonacci |
| 57 | +} // namespace math |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Self-test implementations |
| 61 | + * @returns void |
| 62 | + */ |
| 63 | +static void test() { |
| 64 | + // n = 1 m = 1 return [1, 1] |
| 65 | + std::cout << "1st test"; |
| 66 | + std::vector<uint64_t> arr1 = math::n_bonacci::N_bonacci( |
| 67 | + 1, 1); // first input is the param n and second one is the param m for |
| 68 | + // N-bonacci func |
| 69 | + std::vector<uint64_t> output_array1 = { |
| 70 | + 1, 1}; // It is the expected output series of length m |
| 71 | + assert(std::equal(std::begin(arr1), std::end(arr1), |
| 72 | + std::begin(output_array1))); |
| 73 | + std::cout << "passed" << std::endl; |
| 74 | + |
| 75 | + // n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, |
| 76 | + // 464] |
| 77 | + std::cout << "2nd test"; |
| 78 | + std::vector<uint64_t> arr2 = math::n_bonacci::N_bonacci( |
| 79 | + 5, 15); // first input is the param n and second one is the param m for |
| 80 | + // N-bonacci func |
| 81 | + std::vector<uint64_t> output_array2 = { |
| 82 | + 0, 0, 0, 0, 1, 1, 2, 4, |
| 83 | + 8, 16, 31, 61, 120, 236, 464}; // It is the expected output series of |
| 84 | + // length m |
| 85 | + assert(std::equal(std::begin(arr2), std::end(arr2), |
| 86 | + std::begin(output_array2))); |
| 87 | + std::cout << "passed" << std::endl; |
| 88 | + |
| 89 | + // n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, |
| 90 | + // 492, 976] |
| 91 | + std::cout << "3rd test"; |
| 92 | + std::vector<uint64_t> arr3 = math::n_bonacci::N_bonacci( |
| 93 | + 6, 17); // first input is the param n and second one is the param m for |
| 94 | + // N-bonacci func |
| 95 | + std::vector<uint64_t> output_array3 = { |
| 96 | + 0, 0, 0, 0, 0, 1, 1, 2, 4, |
| 97 | + 8, 16, 32, 63, 125, 248, 492, 976}; // It is the expected output series |
| 98 | + // of length m |
| 99 | + assert(std::equal(std::begin(arr3), std::end(arr3), |
| 100 | + std::begin(output_array3))); |
| 101 | + std::cout << "passed" << std::endl; |
| 102 | + |
| 103 | + // n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 104 | + std::cout << "4th test"; |
| 105 | + std::vector<uint64_t> arr4 = math::n_bonacci::N_bonacci( |
| 106 | + 56, 15); // first input is the param n and second one is the param m |
| 107 | + // for N-bonacci func |
| 108 | + std::vector<uint64_t> output_array4 = { |
| 109 | + 0, 0, 0, 0, 0, 0, 0, 0, |
| 110 | + 0, 0, 0, 0, 0, 0, 0}; // It is the expected output series of length m |
| 111 | + assert(std::equal(std::begin(arr4), std::end(arr4), |
| 112 | + std::begin(output_array4))); |
| 113 | + std::cout << "passed" << std::endl; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @brief Main function |
| 118 | + * @returns 0 on exit |
| 119 | + */ |
| 120 | +int main() { |
| 121 | + test(); // run self-test implementations |
| 122 | + return 0; |
| 123 | +} |
0 commit comments