From 32cce8fe6a4b35d24d6a63b3226daee2a2753684 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sat, 21 Dec 2024 17:38:19 +0100 Subject: Added power operator --- test | 8 ++++++++ zmodn.h | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/test b/test index b36c238..4b426c2 100755 --- 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 index b3b128e..fdb1ee6 100644 --- 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; } -- cgit v1.2.3