diff --git a/math/n_bonacci.cpp b/math/n_bonacci.cpp index 336f51ee2..01b8b1b00 100644 --- a/math/n_bonacci.cpp +++ b/math/n_bonacci.cpp @@ -1,6 +1,7 @@ /** * @file - * @brief Implementation of the [N-bonacci](http://oeis.org/wiki/N-bonacci_numbers) series + * @brief Implementation of the + * [N-bonacci](http://oeis.org/wiki/N-bonacci_numbers) series * * @details * In general, in N-bonacci sequence, @@ -30,7 +31,8 @@ namespace math { */ namespace n_bonacci { /** - * @brief Finds the N-Bonacci series for the `n` parameter value and `m` parameter terms + * @brief Finds the N-Bonacci series for the `n` parameter value and `m` + * parameter terms * @param n is in the N-Bonacci series * @param m is the number of terms in the N-Bonacci sequence * @returns the n-bonacci sequence as vector array @@ -38,10 +40,13 @@ namespace n_bonacci { std::vector N_bonacci(int n, unsigned int m) { std::vector a(m, 0); // we create an empty array of size m - a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of preceding N zeros - a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is also 1 + a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of + /// preceding N zeros + a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is + /// also 1 for (int i = n + 1; i < m; i++) { - // 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 + // 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 // term can be computed using the given formula a[i] = 2 * a[i - 1] - a[i - 1 - n]; } @@ -57,32 +62,51 @@ std::vector N_bonacci(int n, unsigned int m) { static void test() { // n = 1 m = 1 return [1, 1] std::cout << "1st test"; - std::vector 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 - std::vector output_array1 = {1, 1}; // It is the expected output series of length m + std::vector 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 + std::vector output_array1 = { + 1, 1}; // It is the expected output series of length m assert(std::equal(std::begin(arr1), std::end(arr1), std::begin(output_array1))); std::cout << "passed" << std::endl; - // n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464] + // n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, + // 464] std::cout << "2nd test"; - std::vector 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 - std::vector 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 + std::vector 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 + std::vector 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 assert(std::equal(std::begin(arr2), std::end(arr2), std::begin(output_array2))); std::cout << "passed" << std::endl; - // n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976] + // n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, + // 492, 976] std::cout << "3rd test"; - std::vector 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 - std::vector 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 + std::vector 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 + std::vector 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 assert(std::equal(std::begin(arr3), std::end(arr3), std::begin(output_array3))); std::cout << "passed" << std::endl; // n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] std::cout << "4th test"; - std::vector 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 - std::vector 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 + std::vector 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 + std::vector 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 assert(std::equal(std::begin(arr4), std::end(arr4), std::begin(output_array4))); std::cout << "passed" << std::endl;