1 #ifndef MATHUTILITIES_H
2 #define MATHUTILITIES_H
15 template <
typename IntegralType, Traits::EnableIf<std::is_
integral<IntegralType>> * =
nullptr>
16 constexpr IntegralType
digitsum(IntegralType number, IntegralType base = 10)
29 template <
typename IntegralType, Traits::EnableIf<std::is_
integral<IntegralType>> * =
nullptr> constexpr IntegralType
factorial(IntegralType number)
32 for (IntegralType
i = 1;
i <= number; ++
i) {
41 template <
typename IntegralType, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
unsigned<IntegralType>> * =
nullptr>
42 constexpr IntegralType
powerModulo(
const IntegralType base,
const IntegralType exponent,
const IntegralType module)
45 for (IntegralType mask = static_cast<IntegralType>(1) << static_cast<IntegralType>(
sizeof(IntegralType) * 8 - 1); mask; mask >>= 1) {
46 if (mask & exponent) {
60 template <
typename IntegralType, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
unsigned<IntegralType>> * =
nullptr>
61 constexpr IntegralType
inverseModulo(IntegralType number, IntegralType module)
63 IntegralType y1 = 0, y2 = 1;
65 IntegralType tmp = y1 - (module / number) * y2;
68 tmp = module % number;
78 template <
typename IntegralType, Traits::EnableIf<std::is_
integral<IntegralType>, std::is_
unsigned<IntegralType>> * =
nullptr>
79 constexpr IntegralType
orderModulo(
const IntegralType number,
const IntegralType module)
81 IntegralType order = 1;
82 for (;
powerModulo(number, order, module) != 1 && order != module; ++order)
84 return order != module ? order : 0;
88 template <
typename T> constexpr T
min(T first, T second)
90 return first < second ? first : second;
94 template <
typename T1,
typename... T2> constexpr T1
min(T1 first, T1 second, T2... remaining)
96 return first < second ?
min(first, remaining...) :
min(second, remaining...);
100 template <
typename T> constexpr T
max(T first, T second)
102 return first > second ? first : second;
106 template <
typename T1,
typename... T2> constexpr T1
max(T1 first, T1 second, T2... remaining)
108 return first > second ?
max(first, remaining...) :
max(second, remaining...);
113 #endif // MATHUTILITIES_H