The Euclidean algorithm
The Euclidean algorithm
integers. GCD of two numbers is the largest number that divides both of them. A simple way
to find GCD is to factorize both numbers and multiply common prime factors.
// Driver code
int main()
{
int a = 10, b = 15;
// Function call
printf("GCD(%d, %d) = %d\n", a, b, gcd(a, b));
a = 35, b = 10;
printf("GCD(%d, %d) = %d\n", a, b, gcd(a, b));
a = 31, b = 2;
printf("GCD(%d, %d) = %d\n", a, b, gcd(a, b));
return 0;
}
Output
GCD(10, 15) = 5
GCD(35, 10) = 5
GCD(31, 2) = 1
return gcd;
}
// Driver Program
int main()
{
int x, y;
int a = 35, b = 15;
int g = gcdExtended(a, b, &x, &y);
printf("gcd(%d, %d) = %d", a, b, g);
return 0;
}
Output :
gcd(35, 15) = 5
Time Complexity: O(log N)
Auxiliary Space: O(log N)
Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30.
Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15.
Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0.
The greatest common divisor of 210 and 45 is 15.
the algorithm, which supposedly finds the greatest common divisor (GCD) of two integers.
This "greatest common divisor" must exist, since positive integer divisors of integers can't be
any larger than the integers:
Formal description of the Euclidean algorithm
Euclidean algorithm
The Euclidean algorithm, often known as Euclid's algorithm, is an effective way to determine
the greatest common divisor (GCD), or the biggest number that divides two integers
(numbers) evenly and without leaving a remainder. It was initially described in the 300 BC
book The Elements by the Greek mathematician Euclid, for whom it was called. One of the
first algorithms still in use, an algorithm is a step-by-step process for carrying out a
computation in accordance with predetermined principles. It is a component of several
number-theoretic and cryptographic calculations and may be used to simplify fractions. There
are several theoretical and real-world uses for the Euclidean algorithm. It is employed in
conducting division in modular arithmetic as well as simplifying fractions. This algorithm is
employed in both strategies for cracking these cryptosystems by factoring very big composite
numbers as well as the cryptographic protocols that safeguard internet communications.
The greatest common divisor of two positive integers can be discovered using the Euclidean
method. The greatest integer that divides two numbers evenly is called the GCD. Factorizing
both integers and multiplying common prime factors is an easy approach to find GCD.
GCD's fundamental Euclidean algorithm:
The following facts form the basis of the algorithm.
o GCD remains same if we lower a larger integer by subtracting a smaller one from it.
Therefore, if we continually deduct the greater of two, we arrive at GCD.
o Now, if we divide the smaller number rather of subtracting it, the process halts when
we reach the final result of 0.
A recursive function to calculate GCD using Euclid's approach is provided below:
// C++ program to demonstrate
// Basic Euclidean Algorithm
#include <bits/stdc++.h>
using namespace std;
// Function to return
// gcd of a and b
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// Driver Code
int main()
{
int a = 10, b = 15;
// Function call
cout << "GCD(" << a << ", " << b << ") = " << gcd(a, b)
<< endl;
a = 35, b = 10;
cout << "GCD(" << a << ", " << b << ") = " << gcd(a, b)
<< endl;
a = 31, b = 2;
cout << "GCD(" << a << ", " << b << ") = " << gcd(a, b)
<< endl;
return 0;
}
Output
GCD(10, 15) = 5
GCD(35, 10) = 5
GCD(31, 2) = 1
Time Complexity: O (Log min(a, b))
Auxiliary Space: O (Log (min(a,b))
Extended Euclidean Algorithm
Additionally, the extended Euclidean method discovers integer coefficients x and y such that
axe + by = gcd(a, b) is true.
Input: a = 30, b = 20
Output: gcd = 10, x = 1, y = -1
(Note that 30*1 + 20*(-1) = 10)
Input: a = 35, b = 15
Output: gcd = 5, x = 1, y = -2
(Note that 35*1 + 15*(-2) = 5)
The results of the recursive call gcd(b%a, a) are used by the extended Euclidean algorithm to
update the results of gcd(a, b). Let x1 and y1 be the recursively derived values for x and y.
Using the equations shown below, x and y are updated.
ax + by = gcd(a, b)
gcd(a, b) = gcd(b%a, a)
gcd(b%a, a) = (b%a)x1 + ay1
ax + by = (b%a)x1 + ay1
ax + by = (b - [b/a] * a)x1 + ay1
ax + by = a(y1 - [b/a] * x1) + bx1
x = y1 - ⌊b/a⌋ * x1
Comparing LHS and RHS,
y = x1
For improving the other we can also use, (For less no. of iterations) GCD(x,y) = GCD(x
%y,y).
For illustration, the Euclidean algorithm can be used to find the greatest common
divisor of a = 1071 and b = 462.
1071 mod 462= 147
462 mod 147= 21
147 mod 21 =0
Since the last remainder is zero, the algorithm ends with 21 as the greatest
common divisor of 1071 and 462.
Naïve Method
// we'll start from 1 to smallest of the two number until we find a number that divides into
both of them