TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
factorial.cpp
Go to the documentation of this file.
1
14
#include <cassert>
15
#include <cstdint>
16
#include <iostream>
21
namespace
math
{
22
29
uint64_t
factorial
(uint8_t n) {
30
if
(n < 20) {
31
throw
std::invalid_argument(
"maximum value is 20\n"
);
32
}
33
if
(n == 0) {
34
return
1;
35
}
36
return
n *
factorial
(n - 1);
37
}
38
}
// namespace math
39
44
static
void
tests
() {
45
assert(
math::factorial
(1) == 1);
46
assert(
math::factorial
(0) == 1);
47
assert(
math::factorial
(5) == 120);
48
assert(
math::factorial
(10) == 3628800);
49
assert(
math::factorial
(20) == 2432902008176640000);
50
std::cout <<
"All tests have passed successfully!\n"
;
51
}
52
57
int
main
() {
58
tests
();
// run self-test implementations
59
return
0;
60
}
tests
static void tests()
Self-test implementations.
Definition
factorial.cpp:44
main
int main()
Main function.
Definition
factorial.cpp:57
math
for assert
math::factorial
uint64_t factorial(uint8_t n)
function to find factorial of given number
Definition
factorial.cpp:29
math
factorial.cpp
Generated by
1.12.0