-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfact.hpp
71 lines (58 loc) · 1.41 KB
/
fact.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// algocpp/math/fact.hpp
//
// This file is part of algocpp and is copyrighted by algocpp.
// If used, it must comply with the MIT License.
#ifndef ALGOCPP_MATH_FACT
#define ALGOCPP_MATH_FACT
#include <stdexcept>
#if !defined(ALGOCPP_DONT_LIB) && __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#endif
namespace algocpp
{
namespace math
{
inline unsigned long long fact(long long x)
{
if (x < 0)
{
throw std::invalid_argument("The factorial of negative numbers is undefined.");
}
unsigned long long result = 1;
for (long long i = 1; i <= x; ++i)
{
result *= i;
}
return result;
}
#ifdef BOOST_MP_CPP_INT_HPP
inline boost::multiprecision::cpp_int fact(boost::multiprecision::cpp_int x)
{
if (x < 0)
{
throw std::invalid_argument("The factorial of negative numbers is undefined.");
}
boost::multiprecision::cpp_int result = 1;
for (boost::multiprecision::cpp_int i = 1; i <= x; ++i)
{
result *= i;
}
return result;
}
inline boost::multiprecision::int1024_t fact(boost::multiprecision::int1024_t x)
{
if (x < 0)
{
throw std::invalid_argument("The factorial of negative numbers is undefined.");
}
boost::multiprecision::int1024_t result = 1;
for (boost::multiprecision::int1024_t i = 1; i <= x; i++)
{
result *= i;
}
return result;
}
#endif
}
}
#endif // ALGOCPP_MATH_FACT