| 1 | #cxx . . $ `<file>/misc/math.h`/ifndef MATHUTILITIES_H |
| 2 | #define MATHUTILITIES_Hcxx . . $ `misc/math.h:2:9`! |
| 3 | |
| 4 | #include "./traits.h"cxx . . $ `<file>/misc/traits.h`/ |
| 5 | |
| 6 | namespace CppUtilitiescxx . . $ CppUtilities/ { |
| 7 | |
| 8 | /*! |
| 9 | * \brief Returns the digitsum of the given \a number using the specified \a base. |
| 10 | */ |
| 11 | template <typename IntegralType, Traitscxx . . $ CppUtilities/Traits/::EnableIfcxx . . $ CppUtilities/Traits/EnableIf#<stdcxx . . $ std/::is_integral<IntegralType>> * = nullptr> |
| 12 | constexpr IntegralType digitsumcxx . . $ CppUtilities/digitsum(f80f7d6260b3ee3a).(IntegralType number, IntegralType base = 10) |
| 13 | { |
| 14 | IntegralType res = 0; |
| 15 | while (number > 0) { |
| 16 | res += number % base; |
| 17 | number /= base; |
| 18 | } |
| 19 | return res; |
| 20 | } |
| 21 | |
| 22 | /*! |
| 23 | * \brief Returns the factorial of the given \a number. |
| 24 | */ |
| 25 | template <typename IntegralType, Traitscxx . . $ CppUtilities/Traits/::EnableIfcxx . . $ CppUtilities/Traits/EnableIf#<stdcxx . . $ std/::is_integral<IntegralType>> * = nullptr> constexpr IntegralType factorialcxx . . $ CppUtilities/factorial(ada6a8422704cf8a).(IntegralType number) |
| 26 | { |
| 27 | IntegralType res = 1; |
| 28 | for (IntegralType i = 1; i <= number; ++i) { |
| 29 | res *= i; |
| 30 | } |
| 31 | return res; |
| 32 | } |
| 33 | |
| 34 | /*! |
| 35 | * \brief Computes \a base power \a exponent modulo \a module. |
| 36 | */ |
| 37 | template <typename IntegralType, Traitscxx . . $ CppUtilities/Traits/::EnableIfcxx . . $ CppUtilities/Traits/EnableIf#<stdcxx . . $ std/::is_integral<IntegralType>, stdcxx . . $ std/::is_unsigned<IntegralType>> * = nullptr> |
| 38 | constexpr IntegralType powerModulocxx . . $ CppUtilities/powerModulo(5eb09607cedc971b).(const IntegralType base, const IntegralType exponent, const IntegralType module) |
| 39 | { |
| 40 | IntegralType res = 1; |
| 41 | for (IntegralType mask = static_cast<IntegralType>(1) << static_cast<IntegralType>(sizeof(IntegralType) * 8 - 1); mask; mask >>= 1) { |
| 42 | if (mask & exponent) { |
| 43 | res *= base; |
| 44 | } |
| 45 | if (mask != 1) { |
| 46 | res *= res; |
| 47 | } |
| 48 | res %= module; |
| 49 | } |
| 50 | return res; |
| 51 | } |
| 52 | |
| 53 | /*! |
| 54 | * \brief Computes the inverse of \a number modulo \a module. |
| 55 | */ |
| 56 | template <typename IntegralType, Traitscxx . . $ CppUtilities/Traits/::EnableIfcxx . . $ CppUtilities/Traits/EnableIf#<stdcxx . . $ std/::is_integral<IntegralType>, stdcxx . . $ std/::is_unsigned<IntegralType>> * = nullptr> |
| 57 | constexpr IntegralType inverseModulocxx . . $ CppUtilities/inverseModulo(f80f7d6260b3ee3a).(IntegralType number, IntegralType module) |
| 58 | { |
| 59 | IntegralType y1 = 0, y2 = 1; |
| 60 | while (number != 1) { |
| 61 | IntegralType tmp = y1 - (module / number) * y2; |
| 62 | y1 = y2; |
| 63 | y2 = tmp; |
| 64 | tmp = module % number; |
| 65 | module = number; |
| 66 | number = tmp; |
| 67 | } |
| 68 | return y2; |
| 69 | } |
| 70 | |
| 71 | /*! |
| 72 | * \brief Computes the order of \a number modulo \a module. |
| 73 | */ |
| 74 | template <typename IntegralType, Traitscxx . . $ CppUtilities/Traits/::EnableIfcxx . . $ CppUtilities/Traits/EnableIf#<stdcxx . . $ std/::is_integral<IntegralType>, stdcxx . . $ std/::is_unsigned<IntegralType>> * = nullptr> |
| 75 | constexpr IntegralType orderModulocxx . . $ CppUtilities/orderModulo(f80f7d6260b3ee3a).(const IntegralType number, const IntegralType module) |
| 76 | { |
| 77 | IntegralType order = 1; |
| 78 | for (; powerModulocxx . . $ CppUtilities/powerModulo(5eb09607cedc971b).(number, order, module) != 1 && order != module; ++order) |
| 79 | ; |
| 80 | return order != module ? order : 0; |
| 81 | } |
| 82 | |
| 83 | /// \brief Returns the smallest of the given items. |
| 84 | template <typename T> constexpr T mincxx . . $ CppUtilities/min(f80f7d6260b3ee3a).(T first, T second) |
| 85 | { |
| 86 | return first < second ? first : second; |
| 87 | } |
| 88 | |
| 89 | /// \brief Returns the smallest of the given items. |
| 90 | template <typename T1, typename... T2> constexpr T1 mincxx . . $ CppUtilities/min(da21754e2b185302).(T1 first, T1 second, T2... remaining) |
| 91 | { |
| 92 | return first < second ? mincxx . . $ CppUtilities/min(da21754e2b185302).(first, remaining...) : mincxx . . $ CppUtilities/min(f80f7d6260b3ee3a).(second, remaining...); |
| 93 | } |
| 94 | |
| 95 | /// \brief Returns the greatest of the given items. |
| 96 | template <typename T> constexpr T maxcxx . . $ CppUtilities/max(f80f7d6260b3ee3a).(T first, T second) |
| 97 | { |
| 98 | return first > second ? first : second; |
| 99 | } |
| 100 | |
| 101 | /// \brief Returns the greatest of the given items. |
| 102 | template <typename T1, typename... T2> constexpr T1 maxcxx . . $ CppUtilities/max(da21754e2b185302).(T1 first, T1 second, T2... remaining) |
| 103 | { |
| 104 | return first > second ? maxcxx . . $ CppUtilities/max(da21754e2b185302).(first, remaining...) : maxcxx . . $ CppUtilities/max(f80f7d6260b3ee3a).(second, remaining...); |
| 105 | } |
| 106 | |
| 107 | } // namespace CppUtilities |
| 108 | |
| 109 | #endif // MATHUTILITIES_H |