Skip to content

Commit ca2aeec

Browse files
Merge branch 'TheAlgorithms:master' into compilers
2 parents 7fd1f42 + f1ff652 commit ca2aeec

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

.github/workflows/awesome_workflow.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Awesome CI Workflow
22
on: [push, pull_request]
33
permissions:
4+
pull-requests: write
45
contents: write
56

67
jobs:
@@ -13,10 +14,10 @@ jobs:
1314
fetch-depth: 0
1415
- uses: actions/setup-python@v4
1516
- name: requirements
16-
run: |
17+
run: |
1718
sudo apt-get -qq update
1819
sudo apt-get -qq install clang-tidy clang-format
19-
# checks are passing with less errors when used with this version.
20+
# checks are passing with less errors when used with this version.
2021
# The default installs v6.0 which did not work out well in my tests
2122
- name: Setup Git Specs
2223
run: |
@@ -33,8 +34,8 @@ jobs:
3334
git diff --diff-filter=dr --name-only origin/master > git_diff.txt
3435
echo "Files changed-- `cat git_diff.txt`"
3536
- name: Configure for static lint checks
36-
# compiling first gives clang-tidy access to all the header files and settings used to compile the programs.
37-
# This will check for macros, if any, on linux and not for Windows. But the use of portability checks should
37+
# compiling first gives clang-tidy access to all the header files and settings used to compile the programs.
38+
# This will check for macros, if any, on linux and not for Windows. But the use of portability checks should
3839
# be able to catch any errors for other platforms.
3940
run: cmake -B build -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
4041
- name: Lint modified files

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)