From c32c1ec3382b07dd38a05c2d715a12fadd2a9417 Mon Sep 17 00:00:00 2001 From: MichalG315 Date: Fri, 18 Jul 2025 20:22:46 +0200 Subject: [PATCH 1/2] nwd works --- homework/nwd-nnw/nwdNww.hpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..3a4af0ea 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,8 +1,25 @@ #pragma once int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + int a = abs(lhs); + int b = abs(rhs); + int temp = 0; + + if ((lhs == 0 && rhs == 1) || (lhs == 1 && rhs == 0)) { + return 1; + } + + if (lhs == 0 && rhs == 0) { + return 0; + } + + while (a % b != 0) { + temp = a % b; + a = b; + b = temp; + } + + return b; } int NWW(int lhs, int rhs) { From 77273d1f98e8e1cb7430d54ac1ba0097fdcce345 Mon Sep 17 00:00:00 2001 From: MichalG315 Date: Fri, 18 Jul 2025 20:27:27 +0200 Subject: [PATCH 2/2] nww works --- homework/nwd-nnw/nwdNww.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 3a4af0ea..d52a163c 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -23,6 +23,12 @@ int NWD(int lhs, int rhs) { } int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + if (lhs == 0 && rhs == 0) { + return 0; + } + + int a = abs(lhs); + int b = abs(rhs); + int nwd = NWD(a, b); + return a * b / nwd; }