diff --git a/math/math.cpp b/math/math.cpp index 30712c8..a7a8e23 100644 --- a/math/math.cpp +++ b/math/math.cpp @@ -9,7 +9,8 @@ */ /*! - * Returns a pseudo random number between \a lowerbounds and \a upperbounds. + * \brief Returns a pseudo random number between \a lowerbounds and \a upperbounds. + * \remarks Might be removed since std::uniform_int_distribution does the same. */ int MathUtilities::random(int lowerbounds, int upperbounds) { @@ -18,7 +19,7 @@ int MathUtilities::random(int lowerbounds, int upperbounds) } /*! - * Returns the digitsum of the given \a number using the specified \a base. + * \brief Returns the digitsum of the given \a number using the specified \a base. */ int MathUtilities::digitsum(int number, int base) { @@ -31,7 +32,7 @@ int MathUtilities::digitsum(int number, int base) } /*! - * Returns the factorial of the given \a number; + * \brief Returns the factorial of the given \a number. */ int MathUtilities::factorial(int number) { @@ -41,3 +42,48 @@ int MathUtilities::factorial(int number) } return res; } + +/*! + * \brief Computes \a base power \a exponent modulo \a module. + */ +uint64 powerModulo(const uint64 base, const uint64 exponent, const uint64 module) +{ + uint64 res = 1; + for(uint64 mask = 0x8000000000000000; mask; mask >>= 1) { + if(mask & exponent) { + res *= base; + } + if(mask != 1) { + res *= res; + } + res %= module; + } + return res; +} + +/*! + * \brief Computes the inverse of \a number modulo \a module. + */ +int64 inverseModulo(int64 number, int64 module) +{ + int64 y1 = 0, y2 = 1, tmp; + while(number != 1) { + tmp = y1 - (module / number) * y2; + y1 = y2; + y2 = tmp; + tmp = module % number; + module = number; + number = tmp; + } + return y2; +} + +/*! + * \brief Computes the order of \a number modulo \a module. + */ +uint64 orderModulo(const uint64 number, const uint64 module) +{ + uint64 order = 1; + for(; powerModulo(number, order, module) != 1; ++order); + return order; +} diff --git a/math/math.h b/math/math.h index 84c426f..639110d 100644 --- a/math/math.h +++ b/math/math.h @@ -2,12 +2,16 @@ #define MATHUTILITIES_H #include "../application/global.h" +#include "../conversion/types.h" namespace MathUtilities { LIB_EXPORT int random(int lowerbounds, int upperbounds); LIB_EXPORT int digitsum(int number, int base = 10); LIB_EXPORT int factorial(int number); +LIB_EXPORT uint64 powerModulo(uint64 base, uint64 expontent, uint64 module); +LIB_EXPORT int64 inverseModulo(int64 number, int64 module); +LIB_EXPORT uint64 orderModulo(uint64 number, uint64 module); }