diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..d52a163c 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,11 +1,34 @@ #pragma once int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + int a = abs(lhs); + int b = abs(rhs); + int temp = 0; + + if ((lhs == 0 && rhs == 1) || (lhs == 1 && rhs == 0)) { + return 1; + } + + if (lhs == 0 && rhs == 0) { + return 0; + } + + while (a % b != 0) { + temp = a % b; + a = b; + b = temp; + } + + return b; } int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + if (lhs == 0 && rhs == 0) { + return 0; + } + + int a = abs(lhs); + int b = abs(rhs); + int nwd = NWD(a, b); + return a * b / nwd; }