Skip to content

Commit 6247d96

Browse files
Merge branch 'master' into add-prime-binary-string
2 parents 1213f92 + 0bd697d commit 6247d96

File tree

3 files changed

+86
-11
lines changed

3 files changed

+86
-11
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

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# The Algorithms - C++ # {#mainpage}
2+
23
<!-- the suffix in the above line is required for doxygen to consider this as the index page of the generated documentation site -->
34

45
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/TheAlgorithms/C-Plus-Plus)
@@ -18,13 +19,13 @@ This repository is a collection of open-source implementation of a variety of al
1819

1920
## Features
2021

21-
* The repository provides implementations of various algorithms in one of the most fundamental general purpose languages - [C++](https://en.wikipedia.org/wiki/C%2B%2B).
22-
* Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
23-
* Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
24-
* Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS, and Ubuntu (Linux) using MSVC 19 2022, AppleClang 14.0.0, and GNU 11.3.0 respectively.
25-
* Strict adherence to [C++17](https://en.wikipedia.org/wiki/C%2B%2B17) standard ensures portability of code to embedded systems as well like [ESP32](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/cplusplus.html#c-language-standard), [ARM Cortex](https://developer.arm.com/documentation/101458/2404/Standards-support/Supported-C-C---standards-in-Arm-C-C---Compiler), etc. with little to no changes.
26-
* Self-checks within programs ensure correct implementations with confidence.
27-
* Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
22+
- The repository provides implementations of various algorithms in one of the most fundamental general purpose languages - [C++](https://en.wikipedia.org/wiki/C%2B%2B).
23+
- Well documented source code with detailed explanations provide a valuable resource for educators and students alike.
24+
- Each source code is atomic using [STL classes](https://en.wikipedia.org/wiki/Standard_Template_Library) and _no external libraries_ are required for their compilation and execution. Thus, the fundamentals of the algorithms can be studied in much depth.
25+
- Source codes are [compiled and tested](https://github.com/TheAlgorithms/C-Plus-Plus/actions?query=workflow%3A%22Awesome+CI+Workflow%22) for every commit on the latest versions of three major operating systems viz., Windows, MacOS, and Ubuntu (Linux) using MSVC 19 2022, AppleClang 15.0.15, and GNU 13.3.0 respectively.
26+
- Strict adherence to [C++17](https://en.wikipedia.org/wiki/C%2B%2B17) standard ensures portability of code to embedded systems as well like [ESP32](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/cplusplus.html#c-language-standard), [ARM Cortex](https://developer.arm.com/documentation/101458/2404/Standards-support/Supported-C-C---standards-in-Arm-C-C---Compiler), etc. with little to no changes.
27+
- Self-checks within programs ensure correct implementations with confidence.
28+
- Modular implementations and OpenSource licensing enable the functions to be utilized conveniently in other applications.
2829

2930
## Documentation
3031

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)