From 7c86a2a65036dd42662cf8da319b98cd5d60627e Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 12 Apr 2024 13:10:30 +0200 Subject: [PATCH] division improvements --- lab2/zad1/include/GF.hpp | 6 +++--- lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lab2/zad1/include/GF.hpp b/lab2/zad1/include/GF.hpp index d600a22..c622079 100644 --- a/lab2/zad1/include/GF.hpp +++ b/lab2/zad1/include/GF.hpp @@ -55,15 +55,15 @@ public: throw std::invalid_argument("division by 0 is illegal"); } - uint inverse = 1; + GF inverse = 1; for (size_t i = 1; i < N; i++) { - if ((rhs._value * i) % N == 1) { + if (rhs * i == GF(1)) { inverse = i; break; } } - return GF((this->_value * inverse) % N); + return GF(*this * inverse); } GF operator/(const uint &rhs) const { diff --git a/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java b/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java index 9a78742..18161e8 100644 --- a/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java +++ b/lab2/zad2/app/src/main/java/pl/jacekpoz/GF.java @@ -59,15 +59,16 @@ public class GF { throw new IllegalArgumentException("division by 0 is illegal"); } - int inverse = 1; + GF inverse = new GF(this.characteristic, 1); for (int i = 1; i < this.characteristic; i++) { - if ((rhs.value * i) % this.characteristic == 1) { - inverse = i; + GF gfI = new GF(this.characteristic, i); + if (rhs.multiply(gfI).equals(new GF(this.characteristic, 1))) { + inverse = gfI; break; } } - return new GF(this.characteristic, (this.value * inverse) % this.characteristic); + return this.multiply(inverse); } public void divideAssign(final GF rhs) throws IllegalArgumentException {