aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-21 14:50:33 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-21 14:50:33 +0100
commit4d9b0b7cb5c749cd1db3828ab9a3475e6c9811b6 (patch)
treea7ad9e29628cdee6561fe9575459bc81587adab2
parent6eaa7b33aa8690f5ba4cee0897d2d05c71c27c20 (diff)
downloadzmodn-4d9b0b7cb5c749cd1db3828ab9a3475e6c9811b6.tar.gz
zmodn-4d9b0b7cb5c749cd1db3828ab9a3475e6c9811b6.zip
simlified extended gcd
-rw-r--r--zmodn.h22
1 files changed, 3 insertions, 19 deletions
diff --git a/zmodn.h b/zmodn.h
index 8981257..562aa22 100644
--- a/zmodn.h
+++ b/zmodn.h
@@ -46,26 +46,10 @@ private:
46 int64_t int64; 46 int64_t int64;
47}; 47};
48 48
49void swapdiv(int64_t& oldx, int64_t& x, int64_t q) {
50 int64_t tmp = x;
51 x = oldx - q*tmp;
52 oldx = tmp;
53}
54
55std::tuple<int64_t, int64_t, int64_t> extended_gcd(int64_t a, int64_t b) { 49std::tuple<int64_t, int64_t, int64_t> extended_gcd(int64_t a, int64_t b) {
56 int64_t oldr = a; 50 if (b == 0) return {a, 1, 0};
57 int64_t r = b; 51 auto [g, x, y] = extended_gcd(b, a%b);
58 int64_t olds = 1; 52 return {g, y, x - y*(a/b)};
59 int64_t s = 0;
60 int64_t oldt = 0;
61 int64_t t = 1;
62 while (r != 0) {
63 auto q = oldr / r;
64 swapdiv(oldr, r, q);
65 swapdiv(olds, s, q);
66 swapdiv(oldt, t, q);
67 }
68 return {oldr, olds, oldt};
69} 53}
70 54
71#endif 55#endif