-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
feat: Add memoized version of factorial #2964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
realstealthninja
merged 24 commits into
TheAlgorithms:master
from
git5v:feature-dp-algo
Aug 23, 2025
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9027c94
Create check_even_odd.cpp
git5v 13d062d
Update check_even_odd.cpp
git5v 030c69c
Create factorial_top_down_dp.cpp
git5v 07c6989
Merge branch 'master' into master
realstealthninja 5f213b9
Delete dynamic_programming/factorial_top_down_dp.cpp
git5v 6210b9e
Create factorial_top_down_dp.cpp
git5v 89d0c9f
Delete bit_manipulation/check_even_odd.cpp
git5v 04c816d
Merge branch 'master' into feature-dp-algo
realstealthninja e3c39de
Update dynamic_programming/factorial_top_down_dp.cpp
git5v 63319b7
Update dynamic_programming/factorial_top_down_dp.cpp
git5v 5bdde22
Update dynamic_programming/factorial_top_down_dp.cpp
git5v 6f121ec
Update factorial_top_down_dp.cpp
git5v 0016ade
Update factorial_top_down_dp.cpp
git5v 22c58f9
Create memoised_factorial.cpp
git5v 413e2d9
Rename memoised_factorial.cpp to factorial_memoization.cpp
git5v 30d49e3
Update factorial_memoization.cpp
git5v 5873ce9
Delete dynamic_programming/factorial_top_down_dp.cpp
git5v 79e0ba9
Update math/factorial_memoization.cpp
git5v fb3f95e
Update factorial_memoization.cpp
git5v e963491
fix: wrap factorial functions under math namespace
realstealthninja c7c9110
chore: add scope specifier in test cases
realstealthninja 0cbbf30
doc: add documentation to headers
realstealthninja 8a683d2
doc: add defintion of memoisation as well as rewrite brief
realstealthninja f45559d
Merge branch 'master' into feature-dp-algo
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* @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 | ||
git5v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include <array> // For std::array | ||
#include <cstdint> // For uint64_t | ||
|
||
/// Array to store computed factorials for memoization | ||
std::array<uint64_t, 1000> memo{0}; | ||
|
||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* @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]; | ||
} | ||
|
||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} // 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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.