From 890653ca13bb30a9fff5ecf1593c293231b9c54f Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Wed, 12 Aug 2026 13:18:47 +0200 Subject: [PATCH 1/3] Implement NWD feature --- homework/nwd-nnw/nwdNww.hpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..1696b513 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,8 +1,17 @@ #pragma once int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + int first = (lhs > 0) ? lhs : -lhs; + int second = (rhs > 0) ? rhs : -rhs; + + while (second != 0) { + int rest = 0; + rest = first % second; + first = second; + second = rest; + } + + return first; } int NWW(int lhs, int rhs) { From 1322b611addac1c476a4508533c4ccb36715cfd0 Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Wed, 12 Aug 2026 13:48:16 +0200 Subject: [PATCH 2/3] implement 'NWW' feature --- homework/nwd-nnw/nwdNww.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 1696b513..8cdb4cf4 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -15,6 +15,9 @@ 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 result = (lhs / NWD(lhs, rhs))*rhs; // divide first to protect against overflow + return (lhs < 0 ^ rhs < 0) ? -result : result; } From 2d8fcabe1f7dd470fd2500570bb213fb72bf10cf Mon Sep 17 00:00:00 2001 From: Bartekr100 Date: Wed, 12 Aug 2026 13:57:32 +0200 Subject: [PATCH 3/3] Fix formatting of multiplication in NWW function --- homework/nwd-nnw/nwdNww.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 8cdb4cf4..6aeac72a 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -18,6 +18,6 @@ int NWW(int lhs, int rhs) { if (lhs == 0 || rhs == 0) { return 0; } - int result = (lhs / NWD(lhs, rhs))*rhs; // divide first to protect against overflow + int result = (lhs / NWD(lhs, rhs)) * rhs; // divide first to protect against overflow return (lhs < 0 ^ rhs < 0) ? -result : result; }