Files
C-Plus-Plus/math/factorial_memoization.cpp
Vedant Mukhedkar f1ff652601 feat: Add memoized version of factorial (#2964)
* Create check_even_odd.cpp

Implementation to Check if a number is Even or Odd using Bitwise Operator

* Update check_even_odd.cpp

* Create factorial_top_down_dp.cpp

* Delete dynamic_programming/factorial_top_down_dp.cpp

Deleted the one file as there was 2 files in the commit

* Create factorial_top_down_dp.cpp

* Delete bit_manipulation/check_even_odd.cpp

* Update dynamic_programming/factorial_top_down_dp.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update dynamic_programming/factorial_top_down_dp.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update dynamic_programming/factorial_top_down_dp.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update factorial_top_down_dp.cpp

modified

* Update factorial_top_down_dp.cpp

added __uint128_t for handling  fixed-width integer types

* Create memoised_factorial.cpp

* Rename memoised_factorial.cpp to factorial_memoization.cpp

* Update factorial_memoization.cpp

* Delete dynamic_programming/factorial_top_down_dp.cpp

deleted the file from dp folder

* Update math/factorial_memoization.cpp

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>

* Update factorial_memoization.cpp

added cstdint header and switched to uint64 Thanks

* fix: wrap factorial functions under math namespace

* chore: add scope specifier in test cases

* doc: add documentation to headers

* doc: add defintion of memoisation as well as rewrite brief

---------

Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
2025-08-23 14:45:40 +00:00

74 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file
* @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
*
* Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120
*
* 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)
* @author [Vedant Mukhedkar](https://github.com/git5v)
*/
#include <iostream> // for std::cout
#include <cassert> // For test cases
#include <array> // For std::array
#include <cstdint> // For uint64_t
/// Array to store computed factorials for memoization
std::array<uint64_t, 1000> memo{0};
/**
* @namespace math
* @brief Math algorithms
*/
namespace math {
/**
* @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
*/
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
return memo[n];
}
} // namespace math
/**
* @brief Self-test implementations for the fact_recursion function.
* @returns void
*/
void test_fact_recursion() {
// Test cases for factorial computation
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";
}
/**
* @brief Main function to run test cases and interact with the user.
* @returns 0 on program success
*/
int main() {
// Run test cases
test_fact_recursion();
return 0;
}