Skip to content

Commit 3147a4f

Browse files
Revert "fix: remove potential segmentation fault from factorial_memoization.cpp (TheAlgorithms#2994)"
This reverts commit 1426fa4.
1 parent 30be94c commit 3147a4f

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

math/factorial_memoization.cpp

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,73 @@
11
/**
22
* @file
3-
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using
4-
* recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
3+
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
54
* @details
65
* This program computes the factorial of a non-negative integer using recursion
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-
*
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+
*
1315
* Explanation: 5! = 5 × 4 × 3 × 2 × 1 = 120
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.
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.
1819
*
1920
* Time Complexity: O(n)
2021
* Space Complexity: O(n)
22+
* @author [Vedant Mukhedkar](https://github.com/git5v)
2123
*/
2224

25+
#include <iostream> // for std::cout
2326
#include <cassert> // For test cases
27+
#include <array> // For std::array
2428
#include <cstdint> // For uint64_t
25-
#include <vector> // For std::vector
2629

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

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-
};
33+
/**
34+
* @namespace math
35+
* @brief Math algorithms
36+
*/
37+
namespace math {
4338

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);
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];
5049
}
5150

52-
void test_MemorisedFactorial_no_order() {
53-
auto factorial = MemorisedFactorial();
54-
assert(factorial(10) == 3628800);
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";
5563
}
5664

5765
/**
58-
* @brief Main function to run tests
66+
* @brief Main function to run test cases and interact with the user.
5967
* @returns 0 on program success
6068
*/
6169
int main() {
62-
test_MemorisedFactorial_in_order();
63-
test_MemorisedFactorial_no_order();
70+
// Run test cases
71+
test_fact_recursion();
6472
return 0;
6573
}

0 commit comments

Comments
 (0)