diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..17185a24 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,11 +1,25 @@ #pragma once +#include int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + lhs = std::abs(lhs); + rhs = std::abs(rhs); + if (lhs == 0 && rhs != 0) + return rhs; + else if (lhs != 0 && rhs == 0) + return lhs; + while (lhs != rhs) { + if (lhs > rhs) + lhs -= rhs; + else + rhs -= lhs; + } + return lhs; } int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + int nwd = NWD(lhs, rhs); + if (nwd == 0) + return 0; + return std::abs((lhs * rhs) / nwd); }