From 2fc5f482b303653ca3e04133c32251b8d0f82b97 Mon Sep 17 00:00:00 2001 From: PancernyJez Date: Thu, 27 Jun 2024 14:32:27 +0200 Subject: [PATCH] Update nwdNww.hpp --- 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..57154c7e 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,11 +1,28 @@ #pragma once int NWD(int lhs, int rhs) { - // TODO: Implement me :) + int dzielnik {1}; + int cos; + if (lhs == 0 || rhs == 0) { return -1; + } + while (dzielnik < abs(lhs) || dzielnik < abs(rhs)) { + if (lhs % dzielnik == 0 && rhs % dzielnik == 0) { + cos = dzielnik; + dzielnik++; + } else if (lhs % dzielnik != 0 || rhs % dzielnik != 0) { + dzielnik++; + } else + return -1; + } + return cos; } int NWW(int lhs, int rhs) { - // TODO: Implement me :) + if (lhs == 0 || rhs == 0) { return -1; + } else { + int x = NWD(abs(lhs), abs(rhs)); + return lhs * rhs / x; + } }