From 919e46e3d3c2a8db4c4f712dbaa272eb9e5f6634 Mon Sep 17 00:00:00 2001 From: szymon Date: Wed, 16 Sep 2026 23:08:09 +0200 Subject: [PATCH 1/2] implemented gcd and lcm --- homework/nwd-nnw/nwdNww.hpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..df54b9e3 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,11 +1,20 @@ #pragma once int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + // euclidean implementation + int i = 2; + while (rhs != 0) { + if (lhs > rhs) { + lhs %= rhs; + std::swap(lhs, rhs); + } else { + rhs %= lhs; + std::swap(lhs, rhs); + } + } + return lhs; } -int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; +int NWW(int lhs, int rhs) { // kinda lazy but works + return lhs * rhs / (NWD(lhs, rhs)); } From 94b53e7b95da2849f552c2d4d48b61691b63812a Mon Sep 17 00:00:00 2001 From: szymon Date: Wed, 16 Sep 2026 23:29:00 +0200 Subject: [PATCH 2/2] the runner was running for 15 minutes I think there should be some time restraints on tests. --- homework/nwd-nnw/nwdNww.hpp | 38 ++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index df54b9e3..aa61ad38 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,20 +1,36 @@ #pragma once -int NWD(int lhs, int rhs) { - // euclidean implementation - int i = 2; +int NWD(int lhs, int rhs) { // euclidean implementation + if (lhs < 0) { + lhs *= -1; + } + if (rhs < 0) { + rhs *= -1; + } + + if (rhs == lhs) { + return lhs; + } + while (rhs != 0) { - if (lhs > rhs) { - lhs %= rhs; - std::swap(lhs, rhs); - } else { - rhs %= lhs; - std::swap(lhs, rhs); - } + lhs %= rhs; + + std::swap(lhs, rhs); } return lhs; } -int NWW(int lhs, int rhs) { // kinda lazy but works +int NWW(int lhs, int rhs) { + if (lhs < 0) { + lhs *= -1; + } + if (rhs < 0) { + rhs *= -1; + } + if (rhs == lhs) { + return lhs; + } + + // kinda lazy but works return lhs * rhs / (NWD(lhs, rhs)); }