From 7fcd58990e51d5e25efe88f856a3965677c1f0b5 Mon Sep 17 00:00:00 2001 From: Sebastian Sosnowski Date: Tue, 6 Jan 2026 17:49:43 +0100 Subject: [PATCH 1/2] Add NWD implementation --- 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..b67a327f 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,8 +1,25 @@ #pragma once +#include int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + if (lhs == 0) + return rhs; + if (rhs == 0) + return lhs; + int a{lhs}, b{rhs}, quotient{}, reminder{}; + + do { + quotient = a / b; + reminder = a % b; + if (reminder == 0) { + break; + } + a = b; + b = reminder; + } while (reminder != 0); + + return std::abs(b); + // or just return std::gcd(lhs, rhs); :) } int NWW(int lhs, int rhs) { From 1db3f43238302f811cb0bc24cbb72a57bf481431 Mon Sep 17 00:00:00 2001 From: Sebastian Sosnowski Date: Tue, 6 Jan 2026 17:56:37 +0100 Subject: [PATCH 2/2] Add NWW implementation --- homework/nwd-nnw/nwdNww.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index b67a327f..54672280 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,5 +1,4 @@ #pragma once -#include int NWD(int lhs, int rhs) { if (lhs == 0) @@ -23,6 +22,10 @@ int NWD(int lhs, int rhs) { } int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + if (lhs == 0 or rhs == 0) { + return 0; + } + int res = abs((lhs * rhs) / NWD(lhs, rhs)); + return res; + // or just return std::lcm(lhs, rhs); :) }