Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1ff652

Browse files
git5vrealstealthninja
andauthored
feat: Add memoized version of factorial (TheAlgorithms#2964)
* Create check_even_odd.cpp Implementation to Check if a number is Even or Odd using Bitwise Operator * Update check_even_odd.cpp * Create factorial_top_down_dp.cpp * Delete dynamic_programming/factorial_top_down_dp.cpp Deleted the one file as there was 2 files in the commit * Create factorial_top_down_dp.cpp * Delete bit_manipulation/check_even_odd.cpp * Update dynamic_programming/factorial_top_down_dp.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update dynamic_programming/factorial_top_down_dp.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update dynamic_programming/factorial_top_down_dp.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update factorial_top_down_dp.cpp modified * Update factorial_top_down_dp.cpp added __uint128_t for handling fixed-width integer types * Create memoised_factorial.cpp * Rename memoised_factorial.cpp to factorial_memoization.cpp * Update factorial_memoization.cpp * Delete dynamic_programming/factorial_top_down_dp.cpp deleted the file from dp folder * Update math/factorial_memoization.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Update factorial_memoization.cpp added cstdint header and switched to uint64 Thanks * fix: wrap factorial functions under math namespace * chore: add scope specifier in test cases * doc: add documentation to headers * doc: add defintion of memoisation as well as rewrite brief --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
1 parent 4d1aa26 commit f1ff652

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

‎math/factorial_memoization.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @file
3+
* @brief [Factorial](https://en.wikipedia.org/wiki/Factorial) calculation using recursion and [memoization](https://en.wikipedia.org/wiki/Memoization)
4+
* @details
5+
* 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+
*
15+
* 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.
19+
*
20+
* Time Complexity: O(n)
21+
* Space Complexity: O(n)
22+
* @author [Vedant Mukhedkar](https://github.com/git5v)
23+
*/
24+
25+
#include <iostream> // for std::cout
26+
#include <cassert> // For test cases
27+
#include <array> // For std::array
28+
#include <cstdint> // For uint64_t
29+
30+
/// Array to store computed factorials for memoization
31+
std::array<uint64_t, 1000> memo{0};
32+
33+
/**
34+
* @namespace math
35+
* @brief Math algorithms
36+
*/
37+
namespace math {
38+
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];
49+
}
50+
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";
63+
}
64+
65+
/**
66+
* @brief Main function to run test cases and interact with the user.
67+
* @returns 0 on program success
68+
*/
69+
int main() {
70+
// Run test cases
71+
test_fact_recursion();
72+
return 0;
73+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /