-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCDDriver.cpp
91 lines (83 loc) · 3.05 KB
/
GCDDriver.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/****************************************************
* Title: SLColson21 CSC2400:Program 1 *
* Author: Sharon Colson *
* Date: August 2023 *
* Purpose: Calculate Greatest Common Divisor *
* Using Various Algorithms *
****************************************************/
#include <iomanip>
#include "GCD.h"
using namespace std;
int main(int argc, char *argv[])
{
// Convert user arguments to integer
int m = atoi(argv[1]);
int n = atoi(argv[2]);
// Declare additional variables
int x(1), y(0);
// Check that the inputs are not both zero.
while (n == 0 && m==0)
{
cout << "Undefined: no GCD exists while the divisor equals 0. \n"
<< "Please choose again. " << endl;
cout << "m = ";
cin >> m;
cout << endl;
cout << "n = ";
cin >> n;
}
// Restate the user's input
cout << endl << endl;
cout << "Input received: " << m << ", " << n
<< endl << endl;
cout << "============================="
<< endl << endl;
// Part 1: extended Euclid's Algorithm
int eegcd = extendedEuclidGCD(m, n, x, y);
cout << "Extended Euclid Algorithm:"
<< endl << endl;
cout << "Finds the greatest common denominators for 2 integers,"
<< endl;
cout << "a and b, and provides x and y such that ax + by = gcd(a,b). "
<< endl << endl;
cout << "gcd(" << m << ", " << n << ") = " << eegcd
<< endl;
cout << "a = " << m << ", b = " << n
<< endl;
cout << "x = " << x << ", y = " << y
<< endl << endl;
cout << "ax + by = gcd(a,b)"
<< endl;
cout << m << "(" << x << ")"
<< " + " << n << "(" << y << ") = " << eegcd
<< endl;
cout << "============================="
<< endl << endl;
// Part 2: Consecutive Integer Checking
int cicgcd = consecutiveIntegerCheckingGCD(m, n);
cout << "Consecutive Integer Checking Algorithm:"
<< endl << endl;
cout << "Finds the greatest common denominators for 2 integers, a and b,"
<< endl;
cout << "by checking every integer from one to the smaller input. "
<< endl << endl;
cout << "gcd(" << m << ", " << n << ") = " << cicgcd
<< endl;
cout << "============================="
<< endl << endl;
// Part 3: Middle School Method
int msmgcd = middleSchoolMethodGCD(m, n);
cout << "Middle School Method Algorithm:"
<< endl << endl;
cout << "Finds the greatest common denominators for 2 integers, a and b,"
<< endl;
cout << "by finding the prime factors of each input and then combining the"
<< endl;
cout << "common factors via multiplication. "
<< endl << endl;
cout << "gcd(" << m << ", " << n << ") = " << msmgcd
<< endl;
cout << "============================="
<< endl << endl;
return 0;
}