forked from coders-school/cpp-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnwdNww.hpp
More file actions
37 lines (34 loc) · 759 Bytes
/
nwdNww.hpp
File metadata and controls
37 lines (34 loc) · 759 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
int NWD(int lhs, int rhs) {
if (lhs < 0)
lhs = lhs * (-1);
else if (rhs < 0)
rhs = rhs * (-1);
//--------------------------------
if (lhs == 0) {
return rhs;
} else if (rhs == 0) {
return lhs;
}
//--------------------------------
if (lhs == rhs) {
return lhs;
}
//--------------------------------
if (lhs > rhs) {
return NWD(lhs - rhs, rhs);
} else {
return NWD(lhs, rhs - lhs);
}
}
int NWW(int lhs, int rhs) {
if (lhs == 0 || rhs == 0) {
return 0;
} else {
auto result = lhs / NWD(lhs, rhs) * rhs;
if (result < 0) {
return (result * (-1));
} else
return result;
}
}