forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extended_Euclidean_Algorithm.cpp
62 lines (53 loc) · 1.1 KB
/
Extended_Euclidean_Algorithm.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
#include <iostream>
#include <tuple>
using namespace std;
int gcd (int a, int b, int &x, int &y)
{
//base case
if (a == 0)
{
x = 0; y = 1;
return b;
}
int x1, y1;
int d = gcd (b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
// Iterative version of Extended Euclidean algorithm
int gcd_iterative(int a, int b, int& x, int& y) {
x = 1, y = 0;
int x1 = 0, y1 = 1, a1 = a, b1 = b;
while (b1) {
int q = a1 / b1;
tie(x, x1) = make_tuple(x1, x - q * x1);
tie(y, y1) = make_tuple(y1, y - q * y1);
tie(a1, b1) = make_tuple(b1, a1 - q * b1);
}
return a1;
}
int main()
{
int x = 0, y = 0, a, b;
cout<<"Enter the numbers whose GCD is to be found:"<<endl;
cin>>a>>b;
cout<<"GCD of "<<a<<" and "<<b<<" is:"<<endl;
cout<<gcd(a, b, x, y)<<endl;
cout<<"GCD (iterative method) is : "<<endl;
cout<<gcd_iterative(a,b,x,y)<<endl;
return 0;
}
/*
Input:
Enter the numbers whose GCD is to be found:
36
60
Output:
GCD of 36 and 60 is:
12
GCD (iterative method) is :
12
Time Complexity: O(Log min(a, b))
Space Complexity: O(1)
*/