Euclid's Algorithm for Greatest Common Divisor (GCD)
Euclid's Algorithm for Greatest Common Divisor (GCD)
Euclid's Algorithm is a classic algorithm for finding the greatest common divisor (GCD) of two integers. The GCD is the largest positive integer that divides both numbers without leaving a remainder. Euclid's Algorithm is based on the principle that the GCD of two numbers also divides their difference.
Concept
Euclid's Algorithm uses the following principle:
- The GCD of two numbers
a
andb
is the same as the GCD ofb
anda % b
, where%
denotes the modulus operation.
This process is repeated until one of the numbers becomes zero. At that point, the non-zero number is the GCD.
Algorithm Steps
- Initial Step: Given two numbers
a
andb
. - Modulo Operation: Compute the remainder
r
whena
is divided byb
(i.e.,r = a % b
). - Update Numbers: Replace
a
withb
andb
withr
. - Repeat: Continue the process until
b
becomes zero. - Result: The GCD is the non-zero number.
Code Example
JavaScript Implementation:
/**
* Compute the Greatest Common Divisor (GCD) of two integers using Euclid's Algorithm.
* @param {number} a - The first integer.
* @param {number} b - The second integer.
* @return {number} - The GCD of the two integers.
*/
const gcd = (a, b) => {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
};