aboutsummaryrefslogtreecommitdiff
path: root/zmodn.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-21 18:13:27 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-21 18:13:27 +0100
commit8ca27427af98bd3a82e8c5f4a199513295747930 (patch)
tree6409e8f25209c5719ab17d158f242085b8d1fe9a /zmodn.h
parent771667c2bb52235d6dacf67eab933207b59e167b (diff)
downloadzmodn-8ca27427af98bd3a82e8c5f4a199513295747930.tar.gz
zmodn-8ca27427af98bd3a82e8c5f4a199513295747930.zip
Revert "Fix division"
Actually you cannot do division like that, the result is not unique. This reverts commit 771667c2bb52235d6dacf67eab933207b59e167b.
Diffstat (limited to 'zmodn.h')
-rw-r--r--zmodn.h8
1 files changed, 3 insertions, 5 deletions
diff --git a/zmodn.h b/zmodn.h
index 890fb0e..fdb1ee6 100644
--- a/zmodn.h
+++ b/zmodn.h
@@ -40,15 +40,13 @@ public:
40 bool operator!=(const Zmod& z) const { return value != z.value; } 40 bool operator!=(const Zmod& z) const { return value != z.value; }
41 41
42 std::optional<Zmod> inverse() const { 42 std::optional<Zmod> inverse() const {
43 auto [g, a, b] = extended_gcd(value, N); 43 auto [g, a, _] = extended_gcd(value, N);
44 return g == 1 ? Zmod(a) : std::optional<Zmod>{}; 44 return g == 1 ? Zmod(a) : std::optional<Zmod>{};
45 } 45 }
46 46
47 std::optional<Zmod> operator/(const Zmod& d) const { 47 std::optional<Zmod> operator/(const Zmod& d) const {
48 auto [g, x, y] = extended_gcd(value, d.toint()); 48 auto i = d.inverse();
49 auto [a, b] = std::pair<Zmod, Zmod>{value / g, d.toint() / g}; 49 return i ? (*this) * i.value() : i;
50 auto i = b.inverse();
51 return i ? a * i.value() : i;
52 } 50 }
53 51
54 std::optional<Zmod> operator/=(const Zmod& d) { 52 std::optional<Zmod> operator/=(const Zmod& d) {