diff --git a/homework/nwd-nnw/nwdNww.hpp b/homework/nwd-nnw/nwdNww.hpp index 0491a2c9..aa61ad38 100644 --- a/homework/nwd-nnw/nwdNww.hpp +++ b/homework/nwd-nnw/nwdNww.hpp @@ -1,11 +1,36 @@ #pragma once -int NWD(int lhs, int rhs) { - // TODO: Implement me :) - return -1; +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) { + lhs %= rhs; + + std::swap(lhs, rhs); + } + return lhs; } int NWW(int lhs, int rhs) { - // TODO: Implement me :) - return -1; + 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)); }