Skip to content

Commit 180b83e

Browse files
committed
fix: remove potential segmentation fault from factorial_memoization.cpp
1 parent 63aa859 commit 180b83e

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

math/factorial_memoization.cpp

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,64 @@
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+
* @returns factorial of n
34+
*/
35+
std::uint64_t operator()(std::uint64_t n) {
36+
if (n >= this->known_values.size()) {
37+
this->known_values.push_back(n * this->operator()(n - 1));
38+
}
39+
return this->known_values.at(n);
40+
}
41+
};
3842

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];
43+
void test_MemorisedFactorial_in_order() {
44+
auto factorial = MemorisedFactorial();
45+
assert(factorial(0) == 1);
46+
assert(factorial(1) == 1);
47+
assert(factorial(5) == 120);
48+
assert(factorial(10) == 3628800);
4949
}
5050

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";
51+
void test_MemorisedFactorial_no_order() {
52+
auto factorial = MemorisedFactorial();
53+
assert(factorial(10) == 3628800);
6354
}
6455

6556
/**
66-
* @brief Main function to run test cases and interact with the user.
57+
* @brief Main function to run tests
6758
* @returns 0 on program success
6859
*/
6960
int main() {
70-
// Run test cases
71-
test_fact_recursion();
61+
test_MemorisedFactorial_in_order();
62+
test_MemorisedFactorial_no_order();
7263
return 0;
7364
}

0 commit comments

Comments
 (0)