-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMATH1165_Project1.cpp
64 lines (50 loc) · 2.02 KB
/
MATH1165_Project1.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
// Colby Sheppard
// 801005669
// MATH 1165 Project 1
// Due: 10/4/2019
// Notes: Compiled and tested within Ubuntu Build 18.04
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int gcd(int a, int b, int &x, int &y){ // I calculated the GCD using recursion
int x1, y1, d;
if (b==0){ // If statements used to stop the function if either the a or b inputs are equal to 0
x = 1;
y = 0;
return a;
}
else if(a==0){
x = 0;
y = 1;
return b;
}
d = gcd(b, a%b, x1, y1); // Recursion of variables using b and a mod b to find a common divider
// Also passes values back to x1 and y1 to find the linear combination of a and b for the gcd
x = y1; // Uses recursion as well to find the x and y for the linar combination
y = x1 - (a / b) * y1;
return d; // Returns the final gcd of a and b as variable d to int main()
}
int main() {
int a, b, x, y, d; // Variables for gcd and linear combinations of a and b
cout<<"Enter two integers for which you want to find the GCD:" << endl;
cout<<"a: ";
cin>>a; // Separated input lines for a and b
cout<<"\nb: ";
cin>>b;
if(abs(a) < abs(b)){ // Swaps a and b so they are in numerical order, reduces number of lines of code
swap(a, b);
}
d=gcd(abs(a), abs(b), x, y); // Sends a and b as absolute value to allow for negative inputs
// Also sends variables for x and y using pass by reference in the function
if(a<0){
x = -x; // Takes x and y and outputs the negative of x and y to correct the linear combination if neccessary
}
if(b<0){
y = -y;
}
cout<<"\nThe GCD of " << a << " and " << b << " is equal to " << d << endl;
cout<<"The Linear Combination for the GCD of " << a << " and " << b << " is equal to " << a << "x + " << b << "y, where x equals: " << x << " and y equals: " << y << "." << endl;
cout<< a << "(" << x << ")" <<" + " << b << "(" << y << ")" << " = " << d << endl;
return 0;
}