fix: remove potential segmentation fault from factorial_memoization.cpp (#2994)

This commit is contained in:
Piotr Idzik
2025-09-05 14:31:44 +02:00
committed by GitHub
parent 63aa8595cd
commit 43ce63682a

View File

@@ -1,73 +1,65 @@
/**
* @file
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
* @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
*
* 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.
*
* The program uses a recursive function 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
#include <vector> // For std::vector
/// Array to store computed factorials for memoization
std::array<uint64_t, 1000> memo{0};
class MemorisedFactorial {
std::vector<std::uint64_t> known_values = {1};
/**
* @namespace math
* @brief Math algorithms
*/
namespace math {
public:
/**
* @note This function was intentionally written as recursive
* and it does not handle overflows.
* @returns factorial of n
*/
std::uint64_t operator()(std::uint64_t n) {
if (n >= this->known_values.size()) {
this->known_values.push_back(n * this->operator()(n - 1));
}
return this->known_values.at(n);
}
};
/**
* @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];
void test_MemorisedFactorial_in_order() {
auto factorial = MemorisedFactorial();
assert(factorial(0) == 1);
assert(factorial(1) == 1);
assert(factorial(5) == 120);
assert(factorial(10) == 3628800);
}
} // 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";
void test_MemorisedFactorial_no_order() {
auto factorial = MemorisedFactorial();
assert(factorial(10) == 3628800);
}
/**
* @brief Main function to run test cases and interact with the user.
* @brief Main function to run tests
* @returns 0 on program success
*/
int main() {
// Run test cases
test_fact_recursion();
test_MemorisedFactorial_in_order();
test_MemorisedFactorial_no_order();
return 0;
}