From 9027c94862e2b84bd72ebf9b70ebe7caa79dafdf Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:18:05 +0530 Subject: [PATCH 01/21] Create check_even_odd.cpp Implementation to Check if a number is Even or Odd using Bitwise Operator --- bit_manipulation/check_even_odd.cpp | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 bit_manipulation/check_even_odd.cpp diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp new file mode 100644 index 0000000000..78031cf2da --- /dev/null +++ b/bit_manipulation/check_even_odd.cpp @@ -0,0 +1,88 @@ +/** + * @file + * @brief Implementation to [Check if a number is Even or Odd using Bitwise Operator] + * (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html) + * + * @details + * Given an integer N, determine whether it is even or odd using bitwise manipulation. + * The least significant bit (LSB) of a binary number determines its parity: + * - If the LSB is 0, the number is even. + * - If the LSB is 1, the number is odd. + * + * This can be checked efficiently using the bitwise AND operator (&) with 1. + * - If (N & 1) == 0, N is even. + * - If (N & 1) == 1, N is odd. + * + * Worst Case Time Complexity: O(1) + * Space Complexity: O(1) + * + * @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include /// for assert +#include +#include /// for IO operations +#include + +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { +/** + * @namespace even_odd + * @brief Functions for checking if a number is even or odd using bitwise operations + */ +namespace even_odd { + +/** + * @brief Checks if a number is even or odd using bitwise AND. + * @param N The number to check. + * @returns "Even" if N is even, "Odd" if N is odd. + */ +std::string checkEvenOdd(std::int64_t N) { + return (N & 1) == 0 ? "Even" : "Odd"; +} + +} // namespace even_odd +} // namespace bit_manipulation + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + using bit_manipulation::even_odd::checkEvenOdd; + + // Test Even numbers + assert(checkEvenOdd(0) == "Even"); + assert(checkEvenOdd(2) == "Even"); + assert(checkEvenOdd(100) == "Even"); + assert(checkEvenOdd(-4) == "Even"); + assert(checkEvenOdd(-1000) == "Even"); + + // Test Odd numbers + assert(checkEvenOdd(1) == "Odd"); + assert(checkEvenOdd(3) == "Odd"); + assert(checkEvenOdd(101) == "Odd"); + assert(checkEvenOdd(-5) == "Odd"); + assert(checkEvenOdd(-999) == "Odd"); + + std::cout << "All test cases successfully passed!" << std::endl; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + + // Example usage: + // std::int64_t num; + // std::cout << "Enter a number: "; + // std::cin >> num; + // std::cout << num << " is " << bit_manipulation::even_odd::checkEvenOdd(num) << std::endl; + + return 0; +} From 13d062dedc48e50c995d46de82c72197b91598dd Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:19:07 +0530 Subject: [PATCH 02/21] Update check_even_odd.cpp --- bit_manipulation/check_even_odd.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp index 78031cf2da..0ff874b90e 100644 --- a/bit_manipulation/check_even_odd.cpp +++ b/bit_manipulation/check_even_odd.cpp @@ -39,12 +39,12 @@ namespace even_odd { * @brief Checks if a number is even or odd using bitwise AND. * @param N The number to check. * @returns "Even" if N is even, "Odd" if N is odd. - */ -std::string checkEvenOdd(std::int64_t N) { - return (N & 1) == 0 ? "Even" : "Odd"; -} + */ + std::string checkEvenOdd(std::int64_t N) { + return (N & 1) == 0 ? "Even" : "Odd"; + } -} // namespace even_odd + } // namespace even_odd } // namespace bit_manipulation /** From 030c69c7332fcb8a645a27b64215a3d42500c1ab Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Mon, 30 Jun 2025 18:57:27 +0530 Subject: [PATCH 03/21] Create factorial_top_down_dp.cpp --- dynamic_programming/factorial_top_down_dp.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp new file mode 100644 index 0000000000..a89e7c392f --- /dev/null +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -0,0 +1,64 @@ +/** + * @file + * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) + * @details + * This program computes the factorial of a non-negative integer using recursion + * with memoization (top-down dynamic programming). It stores intermediate results + * to avoid redundant calculations for improved efficiency. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include +#include // For test cases + +/// Array to store computed factorials for memoization +long long memo[1000] = {0}; + +/** + * @brief Computes the factorial of a non-negative integer using recursion and memoization. + * @param n The integer whose factorial is to be computed + * @returns The factorial of n + */ +long long fact_rec(int n) { + if (n == 0) return 1; // Base case: 0! = 1 + if (memo[n] != 0) return memo[n]; // Return already computed value + memo[n] = n * fact_rec(n - 1); // Store and return the computed value + return memo[n]; +} + +/** + * @brief Self-test implementations for the fact_rec function. + * @returns void + */ +void test_fact_rec() { + // Test cases for factorial computation + assert(fact_rec(0) == 1); + assert(fact_rec(1) == 1); + assert(fact_rec(5) == 120); + assert(fact_rec(10) == 3628800); + std::cout << "All test cases passed!\n"; +} + +/** + * @brief Main function to run test cases and interact with the user. + * @returns 0 on program success + */ +int main() { + // Run test cases + test_fact_rec(); + + // User interaction loop + // int n; + // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; + // std::cin >> n; + // if (n < 0) { + // std::cout << "Please enter a non-negative integer only.\n"; + // return 0; + // } + // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; + + return 0; +} From 5f213b98e17c96a3295e6576b18779e0818fe6fe Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 11 Jul 2025 16:23:33 +0530 Subject: [PATCH 04/21] Delete dynamic_programming/factorial_top_down_dp.cpp Deleted the one file as there was 2 files in the commit --- dynamic_programming/factorial_top_down_dp.cpp | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp deleted file mode 100644 index a89e7c392f..0000000000 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/** - * @file - * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) - * @details - * This program computes the factorial of a non-negative integer using recursion - * with memoization (top-down dynamic programming). It stores intermediate results - * to avoid redundant calculations for improved efficiency. - * - * Time Complexity: O(n) - * Space Complexity: O(n) - @author [Vedant Mukhedkar](https://github.com/git5v) - */ - -#include -#include // For test cases - -/// Array to store computed factorials for memoization -long long memo[1000] = {0}; - -/** - * @brief Computes the factorial of a non-negative integer using recursion and memoization. - * @param n The integer whose factorial is to be computed - * @returns The factorial of n - */ -long long fact_rec(int n) { - if (n == 0) return 1; // Base case: 0! = 1 - if (memo[n] != 0) return memo[n]; // Return already computed value - memo[n] = n * fact_rec(n - 1); // Store and return the computed value - return memo[n]; -} - -/** - * @brief Self-test implementations for the fact_rec function. - * @returns void - */ -void test_fact_rec() { - // Test cases for factorial computation - assert(fact_rec(0) == 1); - assert(fact_rec(1) == 1); - assert(fact_rec(5) == 120); - assert(fact_rec(10) == 3628800); - std::cout << "All test cases passed!\n"; -} - -/** - * @brief Main function to run test cases and interact with the user. - * @returns 0 on program success - */ -int main() { - // Run test cases - test_fact_rec(); - - // User interaction loop - // int n; - // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; - // std::cin >> n; - // if (n < 0) { - // std::cout << "Please enter a non-negative integer only.\n"; - // return 0; - // } - // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; - - return 0; -} From 6210b9e20d781a574dfb0c1a14c662e08a2ad42a Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 11 Jul 2025 16:44:27 +0530 Subject: [PATCH 05/21] Create factorial_top_down_dp.cpp --- dynamic_programming/factorial_top_down_dp.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp new file mode 100644 index 0000000000..79bfbbc669 --- /dev/null +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -0,0 +1,64 @@ +/** + * @file + * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) + * @details + * This program computes the factorial of a non-negative integer using recursion + * with memoization (top-down dynamic programming). It stores intermediate results + * to avoid redundant calculations for improved efficiency. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include +#include // For test cases + +/// Array to store computed factorials for memoization +long long memo[1000] = {0}; + +/** + * @brief Computes the factorial of a non-negative integer using recursion and memoization. + * @param n The integer whose factorial is to be computed + * @returns The factorial of n + */ +long long fact_rec(int n) { + if (n == 0) return 1; // Base case: 0! = 1 + if (memo[n] != 0) return memo[n]; // Return already computed value + memo[n] = n * fact_rec(n - 1); // Store and return the computed value + return memo[n]; +} + +/** + * @brief Self-test implementations for the fact_rec function. + * @returns void + */ +void test_fact_rec() { + // Test cases for factorial computation + assert(fact_rec(0) == 1); + assert(fact_rec(1) == 1); + assert(fact_rec(5) == 120); + assert(fact_rec(10) == 3628800); + std::cout << "All test cases passed!\n"; +} + +/** + * @brief Main function to run test cases and interact with the user. + * @returns 0 on program success + */ +int main() { + // Run test cases + test_fact_rec(); + + // User interaction loop + // int n; + // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; + // std::cin >> n; + // if (n < 0) { + // std::cout << "Please enter a non-negative integer only.\n"; + // return 0; + // } + // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; + + return 0; +} From 89d0c9f7b6da3e04132974c4eb38eb7a6712dd7e Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 11 Jul 2025 16:45:12 +0530 Subject: [PATCH 06/21] Delete bit_manipulation/check_even_odd.cpp --- bit_manipulation/check_even_odd.cpp | 88 ----------------------------- 1 file changed, 88 deletions(-) delete mode 100644 bit_manipulation/check_even_odd.cpp diff --git a/bit_manipulation/check_even_odd.cpp b/bit_manipulation/check_even_odd.cpp deleted file mode 100644 index 0ff874b90e..0000000000 --- a/bit_manipulation/check_even_odd.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/** - * @file - * @brief Implementation to [Check if a number is Even or Odd using Bitwise Operator] - * (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html) - * - * @details - * Given an integer N, determine whether it is even or odd using bitwise manipulation. - * The least significant bit (LSB) of a binary number determines its parity: - * - If the LSB is 0, the number is even. - * - If the LSB is 1, the number is odd. - * - * This can be checked efficiently using the bitwise AND operator (&) with 1. - * - If (N & 1) == 0, N is even. - * - If (N & 1) == 1, N is odd. - * - * Worst Case Time Complexity: O(1) - * Space Complexity: O(1) - * - * @author [Vedant Mukhedkar](https://github.com/git5v) - */ - -#include /// for assert -#include -#include /// for IO operations -#include - -/** - * @namespace bit_manipulation - * @brief Bit manipulation algorithms - */ -namespace bit_manipulation { -/** - * @namespace even_odd - * @brief Functions for checking if a number is even or odd using bitwise operations - */ -namespace even_odd { - -/** - * @brief Checks if a number is even or odd using bitwise AND. - * @param N The number to check. - * @returns "Even" if N is even, "Odd" if N is odd. - */ - std::string checkEvenOdd(std::int64_t N) { - return (N & 1) == 0 ? "Even" : "Odd"; - } - - } // namespace even_odd -} // namespace bit_manipulation - -/** - * @brief Self-test implementations - * @returns void - */ -static void test() { - using bit_manipulation::even_odd::checkEvenOdd; - - // Test Even numbers - assert(checkEvenOdd(0) == "Even"); - assert(checkEvenOdd(2) == "Even"); - assert(checkEvenOdd(100) == "Even"); - assert(checkEvenOdd(-4) == "Even"); - assert(checkEvenOdd(-1000) == "Even"); - - // Test Odd numbers - assert(checkEvenOdd(1) == "Odd"); - assert(checkEvenOdd(3) == "Odd"); - assert(checkEvenOdd(101) == "Odd"); - assert(checkEvenOdd(-5) == "Odd"); - assert(checkEvenOdd(-999) == "Odd"); - - std::cout << "All test cases successfully passed!" << std::endl; -} - -/** - * @brief Main function - * @returns 0 on exit - */ -int main() { - test(); // run self-test implementations - - // Example usage: - // std::int64_t num; - // std::cout << "Enter a number: "; - // std::cin >> num; - // std::cout << num << " is " << bit_manipulation::even_odd::checkEvenOdd(num) << std::endl; - - return 0; -} From e3c39de8f98317d6a5e5dc7e4eb657bb2c87be4b Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:16:56 +0530 Subject: [PATCH 07/21] Update dynamic_programming/factorial_top_down_dp.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/factorial_top_down_dp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp index 79bfbbc669..1f472ce7a9 100644 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -8,7 +8,7 @@ * * Time Complexity: O(n) * Space Complexity: O(n) - @author [Vedant Mukhedkar](https://github.com/git5v) + * @author [Vedant Mukhedkar](https://github.com/git5v) */ #include From 63319b79de5e4dd79294526811718abb95526e93 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:17:07 +0530 Subject: [PATCH 08/21] Update dynamic_programming/factorial_top_down_dp.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/factorial_top_down_dp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp index 1f472ce7a9..5f251c499b 100644 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -15,7 +15,7 @@ #include // For test cases /// Array to store computed factorials for memoization -long long memo[1000] = {0}; +std::array memo{0}; /** * @brief Computes the factorial of a non-negative integer using recursion and memoization. From 5bdde22fd037646bf8f5b94ab5053d7796d72afe Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:17:22 +0530 Subject: [PATCH 09/21] Update dynamic_programming/factorial_top_down_dp.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- dynamic_programming/factorial_top_down_dp.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp index 5f251c499b..3f1c8e15b0 100644 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -49,16 +49,5 @@ void test_fact_rec() { int main() { // Run test cases test_fact_rec(); - - // User interaction loop - // int n; - // std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; - // std::cin >> n; - // if (n < 0) { - // std::cout << "Please enter a non-negative integer only.\n"; - // return 0; - // } - // std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; - return 0; } From 6f121ecddfa252eabd81441be78d0bcb31a534bf Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:23:35 +0530 Subject: [PATCH 10/21] Update factorial_top_down_dp.cpp modified --- dynamic_programming/factorial_top_down_dp.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp index 3f1c8e15b0..6d79bc7868 100644 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -13,7 +13,8 @@ #include #include // For test cases - +#include // For uint64_t +#include /// Array to store computed factorials for memoization std::array memo{0}; From 0016ade0817b41f4f056a43cbfae56008cdc91a3 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:18:02 +0530 Subject: [PATCH 11/21] Update factorial_top_down_dp.cpp added __uint128_t for handling fixed-width integer types --- dynamic_programming/factorial_top_down_dp.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp index 6d79bc7868..e3236050c3 100644 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ b/dynamic_programming/factorial_top_down_dp.cpp @@ -13,17 +13,16 @@ #include #include // For test cases -#include // For uint64_t #include /// Array to store computed factorials for memoization -std::array memo{0}; +std::array<__uint128_t, 1000> memo{0}; /** * @brief Computes the factorial of a non-negative integer using recursion and memoization. * @param n The integer whose factorial is to be computed * @returns The factorial of n */ -long long fact_rec(int n) { +__uint128_t fact_rec(__uint128_t n) { if (n == 0) return 1; // Base case: 0! = 1 if (memo[n] != 0) return memo[n]; // Return already computed value memo[n] = n * fact_rec(n - 1); // Store and return the computed value From 22c58f9029716f2e99ebc45b46e345d2a96237ca Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:21:52 +0530 Subject: [PATCH 12/21] Create memoised_factorial.cpp --- math/memoised_factorial.cpp | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 math/memoised_factorial.cpp diff --git a/math/memoised_factorial.cpp b/math/memoised_factorial.cpp new file mode 100644 index 0000000000..e3236050c3 --- /dev/null +++ b/math/memoised_factorial.cpp @@ -0,0 +1,53 @@ +/** + * @file + * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) + * @details + * This program computes the factorial of a non-negative integer using recursion + * with memoization (top-down dynamic programming). It stores intermediate results + * to avoid redundant calculations for improved efficiency. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + * @author [Vedant Mukhedkar](https://github.com/git5v) + */ + +#include +#include // For test cases +#include +/// Array to store computed factorials for memoization +std::array<__uint128_t, 1000> memo{0}; + +/** + * @brief Computes the factorial of a non-negative integer using recursion and memoization. + * @param n The integer whose factorial is to be computed + * @returns The factorial of n + */ +__uint128_t fact_rec(__uint128_t n) { + if (n == 0) return 1; // Base case: 0! = 1 + if (memo[n] != 0) return memo[n]; // Return already computed value + memo[n] = n * fact_rec(n - 1); // Store and return the computed value + return memo[n]; +} + +/** + * @brief Self-test implementations for the fact_rec function. + * @returns void + */ +void test_fact_rec() { + // Test cases for factorial computation + assert(fact_rec(0) == 1); + assert(fact_rec(1) == 1); + assert(fact_rec(5) == 120); + assert(fact_rec(10) == 3628800); + std::cout << "All test cases passed!\n"; +} + +/** + * @brief Main function to run test cases and interact with the user. + * @returns 0 on program success + */ +int main() { + // Run test cases + test_fact_rec(); + return 0; +} From 413e2d943d6dfd18e62b79722b36fccfcab78659 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:24:54 +0530 Subject: [PATCH 13/21] Rename memoised_factorial.cpp to factorial_memoization.cpp --- math/{memoised_factorial.cpp => factorial_memoization.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename math/{memoised_factorial.cpp => factorial_memoization.cpp} (100%) diff --git a/math/memoised_factorial.cpp b/math/factorial_memoization.cpp similarity index 100% rename from math/memoised_factorial.cpp rename to math/factorial_memoization.cpp From 30d49e37c63d1060f45855539073f9ea252da6bb Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:29:22 +0530 Subject: [PATCH 14/21] Update factorial_memoization.cpp --- math/factorial_memoization.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index e3236050c3..047bb4e7e5 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -6,6 +6,14 @@ * with memoization (top-down dynamic programming). It stores intermediate results * to avoid redundant calculations for improved efficiency. * + * Example: + * - Input: n = 5 + * - Output: 120 + * + * Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120 + * + * - The program uses a recursive function fact_rec which caches computed + * * Time Complexity: O(n) * Space Complexity: O(n) * @author [Vedant Mukhedkar](https://github.com/git5v) @@ -22,10 +30,10 @@ std::array<__uint128_t, 1000> memo{0}; * @param n The integer whose factorial is to be computed * @returns The factorial of n */ -__uint128_t fact_rec(__uint128_t n) { +__uint128_t fact_recursion(__uint128_t n) { if (n == 0) return 1; // Base case: 0! = 1 if (memo[n] != 0) return memo[n]; // Return already computed value - memo[n] = n * fact_rec(n - 1); // Store and return the computed value + memo[n] = n * fact_recursion(n - 1); // Store and return the computed value return memo[n]; } @@ -33,12 +41,12 @@ __uint128_t fact_rec(__uint128_t n) { * @brief Self-test implementations for the fact_rec function. * @returns void */ -void test_fact_rec() { +void test_fact_recursion() { // Test cases for factorial computation - assert(fact_rec(0) == 1); - assert(fact_rec(1) == 1); - assert(fact_rec(5) == 120); - assert(fact_rec(10) == 3628800); + assert(fact_recursion(0) == 1); + assert(fact_recursion(1) == 1); + assert(fact_recursion(5) == 120); + assert(fact_recursion(10) == 3628800); std::cout << "All test cases passed!\n"; } @@ -48,6 +56,6 @@ void test_fact_rec() { */ int main() { // Run test cases - test_fact_rec(); + test_fact_recursion(); return 0; } From 5873ce96d7010ed3dd27c52bf30b76da7702a0fa Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Thu, 21 Aug 2025 19:31:26 +0530 Subject: [PATCH 15/21] Delete dynamic_programming/factorial_top_down_dp.cpp deleted the file from dp folder --- dynamic_programming/factorial_top_down_dp.cpp | 53 ------------------- 1 file changed, 53 deletions(-) delete mode 100644 dynamic_programming/factorial_top_down_dp.cpp diff --git a/dynamic_programming/factorial_top_down_dp.cpp b/dynamic_programming/factorial_top_down_dp.cpp deleted file mode 100644 index e3236050c3..0000000000 --- a/dynamic_programming/factorial_top_down_dp.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * @file - * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) - * @details - * This program computes the factorial of a non-negative integer using recursion - * with memoization (top-down dynamic programming). It stores intermediate results - * to avoid redundant calculations for improved efficiency. - * - * Time Complexity: O(n) - * Space Complexity: O(n) - * @author [Vedant Mukhedkar](https://github.com/git5v) - */ - -#include -#include // For test cases -#include -/// Array to store computed factorials for memoization -std::array<__uint128_t, 1000> memo{0}; - -/** - * @brief Computes the factorial of a non-negative integer using recursion and memoization. - * @param n The integer whose factorial is to be computed - * @returns The factorial of n - */ -__uint128_t fact_rec(__uint128_t n) { - if (n == 0) return 1; // Base case: 0! = 1 - if (memo[n] != 0) return memo[n]; // Return already computed value - memo[n] = n * fact_rec(n - 1); // Store and return the computed value - return memo[n]; -} - -/** - * @brief Self-test implementations for the fact_rec function. - * @returns void - */ -void test_fact_rec() { - // Test cases for factorial computation - assert(fact_rec(0) == 1); - assert(fact_rec(1) == 1); - assert(fact_rec(5) == 120); - assert(fact_rec(10) == 3628800); - std::cout << "All test cases passed!\n"; -} - -/** - * @brief Main function to run test cases and interact with the user. - * @returns 0 on program success - */ -int main() { - // Run test cases - test_fact_rec(); - return 0; -} From 79e0ba985b1524d1b16e516e6b722f5fe4ab0211 Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 22 Aug 2025 10:56:56 +0530 Subject: [PATCH 16/21] Update math/factorial_memoization.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- math/factorial_memoization.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index 047bb4e7e5..d21ae8da03 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -21,6 +21,7 @@ #include #include // For test cases +#include #include /// Array to store computed factorials for memoization std::array<__uint128_t, 1000> memo{0}; From fb3f95e4ef1087549ec675ef184e30e33c2120ed Mon Sep 17 00:00:00 2001 From: Vedant Mukhedkar <79326074+git5v@users.noreply.github.com> Date: Fri, 22 Aug 2025 11:00:33 +0530 Subject: [PATCH 17/21] Update factorial_memoization.cpp added cstdint header and switched to uint64 Thanks --- math/factorial_memoization.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index d21ae8da03..0559e73968 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -7,12 +7,13 @@ * to avoid redundant calculations for improved efficiency. * * Example: - * - Input: n = 5 - * - Output: 120 + * Input: n = 5 + * Output: 120 * - * Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120 + * Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120 * - * - The program uses a recursive function fact_rec which caches computed + * The program uses a recursive function fact_recursion which caches computed + * results in a memo array to avoid recalculating factorials for the same numbers. * * Time Complexity: O(n) * Space Complexity: O(n) @@ -21,17 +22,18 @@ #include #include // For test cases -#include -#include +#include +#include // For uint64_t + /// Array to store computed factorials for memoization -std::array<__uint128_t, 1000> memo{0}; +std::array memo{0}; /** * @brief Computes the factorial of a non-negative integer using recursion and memoization. * @param n The integer whose factorial is to be computed * @returns The factorial of n */ -__uint128_t fact_recursion(__uint128_t n) { +uint64_t fact_recursion(uint64_t n) { if (n == 0) return 1; // Base case: 0! = 1 if (memo[n] != 0) return memo[n]; // Return already computed value memo[n] = n * fact_recursion(n - 1); // Store and return the computed value @@ -39,7 +41,7 @@ __uint128_t fact_recursion(__uint128_t n) { } /** - * @brief Self-test implementations for the fact_rec function. + * @brief Self-test implementations for the fact_recursion function. * @returns void */ void test_fact_recursion() { From e9634911724f1b34ff6236dcf89c6bfa46a90d06 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Fri, 22 Aug 2025 14:27:44 +0000 Subject: [PATCH 18/21] fix: wrap factorial functions under math namespace --- math/factorial_memoization.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index 0559e73968..4c88aad762 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -27,6 +27,13 @@ /// Array to store computed factorials for memoization std::array memo{0}; +#include /// for IO operations + +/** + * @namespace math + * @brief Math algorithms + */ +namespace math { /** * @brief Computes the factorial of a non-negative integer using recursion and memoization. @@ -40,6 +47,7 @@ uint64_t fact_recursion(uint64_t n) { return memo[n]; } +} // namespace math /** * @brief Self-test implementations for the fact_recursion function. * @returns void From c7c91106333b5dd7706aa61d5b711daa3096fcc8 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Fri, 22 Aug 2025 14:28:07 +0000 Subject: [PATCH 19/21] chore: add scope specifier in test cases --- math/factorial_memoization.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index 4c88aad762..6d746436c2 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -54,10 +54,10 @@ uint64_t fact_recursion(uint64_t n) { */ void test_fact_recursion() { // Test cases for factorial computation - assert(fact_recursion(0) == 1); - assert(fact_recursion(1) == 1); - assert(fact_recursion(5) == 120); - assert(fact_recursion(10) == 3628800); + assert(math::fact_recursion(0) == 1); + assert(math::fact_recursion(1) == 1); + assert(math::fact_recursion(5) == 120); + assert(math::fact_recursion(10) == 3628800); std::cout << "All test cases passed!\n"; } From 0cbbf30eb578f6d6b8d25a26672c249428192f6b Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Fri, 22 Aug 2025 14:29:46 +0000 Subject: [PATCH 20/21] doc: add documentation to headers --- math/factorial_memoization.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index 6d746436c2..3005ae3bae 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -20,14 +20,13 @@ * @author [Vedant Mukhedkar](https://github.com/git5v) */ -#include +#include // for std::cout #include // For test cases -#include +#include // For std::array #include // For uint64_t /// Array to store computed factorials for memoization std::array memo{0}; -#include /// for IO operations /** * @namespace math From 8a683d2d567dd46d22e9d6070b5ceec9b5f6cf06 Mon Sep 17 00:00:00 2001 From: realstealthninja <68815218+realstealthninja@users.noreply.github.com> Date: Fri, 22 Aug 2025 14:33:22 +0000 Subject: [PATCH 21/21] doc: add defintion of memoisation as well as rewrite brief --- math/factorial_memoization.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/math/factorial_memoization.cpp b/math/factorial_memoization.cpp index 3005ae3bae..c53df6e498 100644 --- a/math/factorial_memoization.cpp +++ b/math/factorial_memoization.cpp @@ -1,11 +1,13 @@ /** * @file - * @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) + * @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization) * @details * This program computes the factorial of a non-negative integer using recursion * with memoization (top-down dynamic programming). It stores intermediate results * to avoid redundant calculations for improved efficiency. * + * Memoization is a form of caching where the result to an expensive function call + * is stored and returned. * Example: * Input: n = 5 * Output: 120