Skip to content

Commit 43ce636

Browse files
authored
fix: remove potential segmentation fault from factorial_memoization.cpp (TheAlgorithms#2994)
1 parent 63aa859 commit 43ce636

File tree

1 file changed

+40
-48
lines changed

1 file changed

+40
-48
lines changed

math/factorial_memoization.cpp

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,65 @@
11
/**
22
* @file
3-
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
3+
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using
4+
* recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
45
* @details
56
* This program computes the factorial of a non-negative integer using recursion
6-
* with memoization (top-down dynamic programming). It stores intermediate results
7-
* to avoid redundant calculations for improved efficiency.
8-
*
9-
* Memoization is a form of caching where the result to an expensive function call
10-
* is stored and returned.
11-
* Example:
12-
* Input: n = 5
13-
* Output: 120
14-
*
7+
* with memoization (top-down dynamic programming). It stores intermediate
8+
* results to avoid redundant calculations for improved efficiency.
9+
*
10+
* Memoization is a form of caching where the result to an expensive function
11+
* call is stored and returned. Example: Input: n = 5 Output: 120
12+
*
1513
* Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120
16-
*
17-
* The program uses a recursive function fact_recursion which caches computed
18-
* results in a memo array to avoid recalculating factorials for the same numbers.
14+
*
15+
* The program uses a recursive function which caches computed
16+
* results in a memo array to avoid recalculating factorials for the same
17+
* numbers.
1918
*
2019
* Time Complexity: O(n)
2120
* Space Complexity: O(n)
22-
* @author [Vedant Mukhedkar](https://github.com/git5v)
2321
*/
2422

25-
#include <iostream> // for std::cout
2623
#include <cassert> // For test cases
27-
#include <array> // For std::array
2824
#include <cstdint> // For uint64_t
25+
#include <vector> // For std::vector
2926

30-
/// Array to store computed factorials for memoization
31-
std::array<uint64_t, 1000> memo{0};
27+
class MemorisedFactorial {
28+
std::vector<std::uint64_t> known_values = {1};
3229

33-
/**
34-
* @namespace math
35-
* @brief Math algorithms
36-
*/
37-
namespace math {
30+
public:
31+
/**
32+
* @note This function was intentionally written as recursive
33+
* and it does not handle overflows.
34+
* @returns factorial of n
35+
*/
36+
std::uint64_t operator()(std::uint64_t n) {
37+
if (n >= this->known_values.size()) {
38+
this->known_values.push_back(n * this->operator()(n - 1));
39+
}
40+
return this->known_values.at(n);
41+
}
42+
};
3843

39-
/**
40-
* @brief Computes the factorial of a non-negative integer using recursion and memoization.
41-
* @param n The integer whose factorial is to be computed
42-
* @returns The factorial of n
43-
*/
44-
uint64_t fact_recursion(uint64_t n) {
45-
if (n == 0) return 1; // Base case: 0! = 1
46-
if (memo[n] != 0) return memo[n]; // Return already computed value
47-
memo[n] = n * fact_recursion(n - 1); // Store and return the computed value
48-
return memo[n];
44+
void test_MemorisedFactorial_in_order() {
45+
auto factorial = MemorisedFactorial();
46+
assert(factorial(0) == 1);
47+
assert(factorial(1) == 1);
48+
assert(factorial(5) == 120);
49+
assert(factorial(10) == 3628800);
4950
}
5051

51-
} // namespace math
52-
/**
53-
* @brief Self-test implementations for the fact_recursion function.
54-
* @returns void
55-
*/
56-
void test_fact_recursion() {
57-
// Test cases for factorial computation
58-
assert(math::fact_recursion(0) == 1);
59-
assert(math::fact_recursion(1) == 1);
60-
assert(math::fact_recursion(5) == 120);
61-
assert(math::fact_recursion(10) == 3628800);
62-
std::cout << "All test cases passed!\n";
52+
void test_MemorisedFactorial_no_order() {
53+
auto factorial = MemorisedFactorial();
54+
assert(factorial(10) == 3628800);
6355
}
6456

6557
/**
66-
* @brief Main function to run test cases and interact with the user.
58+
* @brief Main function to run tests
6759
* @returns 0 on program success
6860
*/
6961
int main() {
70-
// Run test cases
71-
test_fact_recursion();
62+
test_MemorisedFactorial_in_order();
63+
test_MemorisedFactorial_no_order();
7264
return 0;
7365
}

0 commit comments

Comments
 (0)