aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-21 17:38:19 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-21 17:38:19 +0100
commit32cce8fe6a4b35d24d6a63b3226daee2a2753684 (patch)
tree6409e8f25209c5719ab17d158f242085b8d1fe9a
parent7e705a3342bc36f9f02b4e8e0050bc5842a62f9b (diff)
downloadzmodn-32cce8fe6a4b35d24d6a63b3226daee2a2753684.tar.gz
zmodn-32cce8fe6a4b35d24d6a63b3226daee2a2753684.zip
Added power operator
-rwxr-xr-xtest8
-rw-r--r--zmodn.h7
2 files changed, 15 insertions, 0 deletions
diff --git a/test b/test
index b36c238..4b426c2 100755
--- a/test
+++ b/test
@@ -133,6 +133,14 @@ public:
133 assert_equal(n.toint(), 2); 133 assert_equal(n.toint(), 2);
134 } 134 }
135}, 135},
136{
137 .name = "2^12 mod 154",
138 .f = []() {
139 Zmod<154> b = 2;
140 auto p = b ^ 12;
141 assert_equal(p, 92);
142 }
143},
136}; 144};
137 145
138int main() { 146int main() {
diff --git a/zmodn.h b/zmodn.h
index b3b128e..fdb1ee6 100644
--- a/zmodn.h
+++ b/zmodn.h
@@ -25,10 +25,17 @@ public:
25 Zmod operator+(const Zmod& z) const { return value + z.value; } 25 Zmod operator+(const Zmod& z) const { return value + z.value; }
26 Zmod operator-(const Zmod& z) const { return value - z.value; } 26 Zmod operator-(const Zmod& z) const { return value - z.value; }
27 Zmod operator*(const Zmod& z) const { return value * z.value; } 27 Zmod operator*(const Zmod& z) const { return value * z.value; }
28
28 Zmod operator+=(const Zmod& z) { return (*this) = value + z.value; } 29 Zmod operator+=(const Zmod& z) { return (*this) = value + z.value; }
29 Zmod operator-=(const Zmod& z) { return (*this) = value - z.value; } 30 Zmod operator-=(const Zmod& z) { return (*this) = value - z.value; }
30 Zmod operator*=(const Zmod& z) { return (*this) = value * z.value; } 31 Zmod operator*=(const Zmod& z) { return (*this) = value * z.value; }
31 32
33 Zmod operator^(decltype(N) z) const {
34 if (z == 0) return 1;
35 if (z % 2 == 0) return (((*this) * (*this)) ^ (z/2));
36 return (*this) * ((*this) ^ (z-1));
37 }
38
32 bool operator==(const Zmod& z) const { return value == z.value; } 39 bool operator==(const Zmod& z) const { return value == z.value; }
33 bool operator!=(const Zmod& z) const { return value != z.value; } 40 bool operator!=(const Zmod& z) const { return value != z.value; }
34 41