-
Notifications
You must be signed in to change notification settings - Fork 6
/
IntegerFunctions.hpp
66 lines (45 loc) · 1.47 KB
/
IntegerFunctions.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
#pragma once
#include <concepts>
#include <cstddef>
#include <unordered_map>
#include <vector>
#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/numbers/INumber.hpp"
#include "fintamath/numbers/Integer.hpp"
namespace fintamath {
using FactorToCountMap = std::unordered_map<Integer, Integer>;
template <std::derived_from<INumber> Lhs>
Lhs pow(const Lhs &lhs, Integer rhs) {
if (lhs == 0 && rhs == 0) {
throw UndefinedException("pow({}, {}) is undefined (zero to the power of zero)");
}
if (rhs < 0) {
return pow(1 / lhs, -rhs);
}
// Use exponentiation by squaring with constant auxiliary memory (iterative version).
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring#With_constant_auxiliary_memory.
Lhs res(1);
Lhs sqr = lhs;
while (rhs != 0) {
if (rhs % 2 == 0) {
rhs /= 2;
sqr = sqr * sqr;
}
else {
--rhs;
res = res * sqr;
}
}
return res;
}
Integer abs(const Integer &rhs);
Integer gcd(const Integer &lhs, const Integer &rhs);
Integer lcm(const Integer &lhs, const Integer &rhs);
Integer sqrt(const Integer &rhs);
Integer sqrt(const Integer &rhs, Integer &remainder);
Integer factorial(const Integer &rhs);
Integer factorial(const Integer &rhs, size_t order);
FactorToCountMap factors(Integer rhs, Integer limit = -1);
Integer combinations(const Integer &totalNumber, const Integer &choosedNumber);
Integer multinomialCoefficient(const std::vector<Integer> &groupNumbers);
}