-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcdprg.cpp
More file actions
57 lines (48 loc) · 1.03 KB
/
gcdprg.cpp
File metadata and controls
57 lines (48 loc) · 1.03 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;
class GCD {
int *a, *b;
public:
GCD() {
a = new int;
b = new int;
}
void input() {
cout << "Enter two numbers: ";
cin >> *a >> *b;
}
int findGCD_naive() {
int gcd = 1;
int limit = (*a < *b) ? *a : *b;
for (int i = 1; i <= limit; i++) {
if ((*a % i == 0) && (*b % i == 0)) {
gcd = i;
}
}
return gcd;
}
int findGCD_euclidean(int x, int y) {
while (y != 0) {
int temp = y;
y = x % y;
x = temp;
}
return x;
}
void displayGCDs() {
int naiveGCD = findGCD_naive();
int optimalGCD = findGCD_euclidean(*a, *b);
cout << "Naive GCD: " << naiveGCD << endl;
cout << "Optimal GCD (Euclidean Algorithm): " << optimalGCD << endl;
}
~GCD() {
delete a;
delete b;
}
};
int main() {
GCD obj;
obj.input();
obj.displayGCDs();
return 0;
}