commit 32cce8fe6a4b35d24d6a63b3226daee2a2753684
parent 7e705a3342bc36f9f02b4e8e0050bc5842a62f9b
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Sat, 21 Dec 2024 17:38:19 +0100
Added power operator
Diffstat:
2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/test b/test
@@ -133,6 +133,14 @@ public:
assert_equal(n.toint(), 2);
}
},
+{
+ .name = "2^12 mod 154",
+ .f = []() {
+ Zmod<154> b = 2;
+ auto p = b ^ 12;
+ assert_equal(p, 92);
+ }
+},
};
int main() {
diff --git a/zmodn.h b/zmodn.h
@@ -25,10 +25,17 @@ public:
Zmod operator+(const Zmod& z) const { return value + z.value; }
Zmod operator-(const Zmod& z) const { return value - z.value; }
Zmod operator*(const Zmod& z) const { return value * z.value; }
+
Zmod operator+=(const Zmod& z) { return (*this) = value + z.value; }
Zmod operator-=(const Zmod& z) { return (*this) = value - z.value; }
Zmod operator*=(const Zmod& z) { return (*this) = value * z.value; }
+ Zmod operator^(decltype(N) z) const {
+ if (z == 0) return 1;
+ if (z % 2 == 0) return (((*this) * (*this)) ^ (z/2));
+ return (*this) * ((*this) ^ (z-1));
+ }
+
bool operator==(const Zmod& z) const { return value == z.value; }
bool operator!=(const Zmod& z) const { return value != z.value; }