From f7e28d4ce6d3f7bbaa72d7e069e665557c80b3ac Mon Sep 17 00:00:00 2001 From: ctrl-daan Date: Mon, 10 Mar 2025 14:08:39 +0100 Subject: [PATCH 1/2] NWD function added, test is passing --- homework/nwd-nnw/nwdNww.hpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..1d74e0ed 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,8 +1,27 @@ #pragma once int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + if (lhs < 0) + lhs = lhs * (-1); + else if (rhs < 0) + rhs = rhs * (-1); + + //-------------------------------- + if (lhs == 0) { + return rhs; + } else if (rhs == 0) { + return lhs; + } + //-------------------------------- + if (lhs == rhs) { + return lhs; + } + //-------------------------------- + if (lhs > rhs) { + return NWD(lhs - rhs, rhs); + } else { + return NWD(lhs, rhs - lhs); + } } int NWW(int lhs, int rhs) { From 9135fe2c5010295c6e793467768614d3c8e0d11b Mon Sep 17 00:00:00 2001 From: ctrl-daan Date: Mon, 10 Mar 2025 14:34:07 +0100 Subject: [PATCH 2/2] NWW function added, test is passing --- homework/nwd-nnw/nwdNww.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 1d74e0ed..4e53e32f 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -25,6 +25,13 @@ int NWD(int lhs, int rhs) { } int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + if (lhs == 0 || rhs == 0) { + return 0; + } else { + auto result = lhs / NWD(lhs, rhs) * rhs; + if (result < 0) { + return (result * (-1)); + } else + return result; + } }