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