Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My solution of NWD and NWW function #1403

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions homework/nwd-nnw/nwdNww.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
#pragma once
#include <cstdlib>

int NWD(int lhs, int rhs) {
// TODO: Implement me :)
return -1;
if (rhs == 0) {
return lhs;
}

int remainderOfDivision = lhs % rhs;
while (remainderOfDivision != 0) {
lhs = rhs;
rhs = remainderOfDivision;
remainderOfDivision = lhs % rhs;
}

return abs(rhs);
}

int NWW(int lhs, int rhs) {
// TODO: Implement me :)
return -1;
if (lhs == 0 || rhs == 0) {
return 0;
}

lhs = abs(lhs);
rhs = abs(rhs);
int result = lhs;
if (rhs > result) {
result = rhs;
}

int remainderOfDivision1 = result % rhs;
int remainderOfDivision2 = result % lhs;
while (remainderOfDivision1 != 0 || remainderOfDivision2 != 0) {
++result;
remainderOfDivision1 = result % rhs;
remainderOfDivision2 = result % lhs;
}

return result;
}
Loading