0% found this document useful (0 votes)
5 views

The Euclidean algorithm

The Euclidean algorithm is a method for finding the greatest common divisor (GCD) of two positive integers, utilizing division and remainder operations. It can be implemented recursively, and an extended version also computes integer coefficients that satisfy the equation ax + by = gcd(a, b). The algorithm is efficient with a time complexity of O(log min(a, b)) and has applications in number theory and cryptography.

Uploaded by

Chitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

The Euclidean algorithm

The Euclidean algorithm is a method for finding the greatest common divisor (GCD) of two positive integers, utilizing division and remainder operations. It can be implemented recursively, and an extended version also computes integer coefficients that satisfy the equation ax + by = gcd(a, b). The algorithm is efficient with a time complexity of O(log min(a, b)) and has applications in number theory and cryptography.

Uploaded by

Chitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

The Euclidean algorithm is a way to find the greatest common divisor of two positive

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.

Basic Euclidean Algorithm for GCD:


The algorithm is based on the below facts.
 If we subtract a smaller number from a larger one (we reduce a larger number),
GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we
end up with GCD.
 Now instead of subtraction, if we divide the smaller number, the algorithm stops
when we find the remainder 0.
Below is a recursive function to evaluate gcd using Euclid’s algorithm:
// C program to demonstrate Basic Euclidean Algorithm
#include <stdio.h>

// 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
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

Time Complexity: O(Log min(a, b))


Auxiliary Space: O(Log (min(a,b))

Extended Euclidean Algorithm:


Extended Euclidean algorithm also finds integer coefficients x and y such that: ax + by =
gcd(a, b)
Examples:
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 extended Euclidean algorithm updates the results of gcd(a, b) using the results calculated
by the recursive call gcd(b%a, a). Let values of x and y calculated by the recursive call be
x1 and y1. x and y are updated using the below expressions.
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
Comparing LHS and RHS,
x = y1 – ?b/a? * x1
y = x1
Below is an implementation of the above approach:
// C program to demonstrate working of extended
// Euclidean Algorithm
#include <stdio.h>

// C function for extended Euclidean Algorithm


int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0;
*y = 1;
return b;
}

int x1, y1; // To store results of recursive call


int gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;

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)

How does Extended Algorithm Work?


As seen above, x and y are results for inputs a and b,
a.x + b.y = gcd —-(1)
And x1 and y1 are results for inputs b%a and a
(b%a).x1 + a.y1 = gcd
When we put b%a = (b – (?b/a?).a) in above,
we get following. Note that ?b/a? is floor(b/a)
(b – (?b/a?).a).x1 + a.y1 = gcd
Above equation can also be written as below
b.x1 + a.(y1 – (?b/a?).x1) = gcd —(2)
After comparing coefficients of ‘a’ and ‘b’ in (1) and
(2), we get following,
x = y1 – ?b/a? * x1
y = x1
How is Extended Algorithm Useful?
The extended Euclidean algorithm is particularly useful when a and b are coprime (or gcd is
1). Since x is the modular multiplicative inverse of “a modulo b”, and y is the modular
multiplicative inverse of “b modulo a”. In particular, the computation of the modular
multiplicative inverse is an essential step in RSA public-key encryption method.

The Euclidean algorithm


The Euclidean algorithm is a way to find the greatest common divisor of two positive
integers, a and b. First let me show the computations for a=210 and b=45.

 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

 Input Two positive integers, a and b.


 Output The greatest common divisor, g, of a and b.
 Internal computation
1. If a<b, exchange a and b.
2. Divide a by b and get the remainder, r. If r=0, report b as the GCD of a and b.

Replace a by b and replace b by r. Return to the previous step.

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

int gcd(int a , int b)


{
int ans = 1;
for(int i;i<=min(a,b);i++){
if(a%i == 0 && b%i == 0){
ans=i;
}
}
}
Euclid’s is better

You might also like