# Finding GCD Programmatically: An Overview Of Euclidean Algorithm And Its Applications

· 2 min read December 16, 2022

The greatest common divisor (GCD) of two integers $a$ and $b$ is defined as the greatest integer that divides both $a$ and $b$ without leaving a remainder. GCD is a fundamental concept in mathematics and has numerous applications in fields such as computer science, cryptography, and data compression. In this blog post, we will explore one of the most widely used methods for calculating GCD - Euclidean algorithm - and discuss its time complexity and practical applications.

## Euclidean Algorithm

Euclidean algorithm is an efficient method for finding the GCD of two numbers. It is based on the observation that if $r$ is the remainder when $a$ is divided by $b$, then the common divisors of $a$ and $b$ are the same as of $b$ and $r$. In other words, $\text{GCD}(a, b) = \text{GCD}(b, r)$. This relationship allows us to find the GCD of $a$ and $b$ by repeatedly applying this formula until the second number becomes zero. At that point, the GCD is the first number in the pair.

Here is a pseudocode implementation of Euclidean algorithm:

function gcd(a, b)
if b = 0
return a
else
return gcd(b, a mod b)


This generates an iterative process that grows as the logarithm of the numbers involved.

## Lame’s Theorem and Order of Growth

Lame’s theorem states that if Euclidean algorithm requires $k$ steps to compute the GCD of a pair $(a, b)$, then the smaller number of the pair (i.e. $\min(a, b)$) must be greater than or equal to the $k$th Fibonacci number. In other words, $\min(a, b) \geq \text{Fib}(k)$.

The order of growth of Euclidean algorithm can be estimated using Lame’s theorem. Let $n$ be the smaller number of the pair $(a, b)$. If the process takes $k$ steps, then we must have $n \geq \text{Fib}(k)$. Using the approximation formula for Fibonacci numbers, we can rewrite this as $n \geq \phi^k / \sqrt{5}$. Removing the insignificant term $1 / \sqrt{5}$ gives us $n \geq \phi^k$, or $\log_{\phi}(n) = k$. Therefore, the order of growth of Euclidean algorithm is $O(\log(n))$.

## Implementation and Applications

Euclidean algorithm can be easily implemented in any programming language. Here is an example in Scheme,python and javascript :

### Implementation In Scheme

(define (gcd a b)
(if (= b 0)
a
(gcd b (remainder a b))))


### Implementation in Python

def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)


### Implementation in JavaScript

function gcd(a, b) {
if (b == 0) {
return a
} else {
return gcd(b, a % b)
}
}


### Applications

Some practical applications of Euclidean algorithm include finding the least common multiple (LCM) of two numbers, checking if two numbers are coprime (i.e. have no common divisors other than 1), and solving linear Diophantine equations.

Euclidean algorithm is also an essential component of many algorithms and protocols in computer science, such as RSA encryption and image compression.

In conclusion, Euclidean algorithm is a simple yet powerful method for finding the GCD of two numbers. Its time complexity of O(log(n)) makes it suitable for use in large-scale problems. Understanding and implementing Euclidean algorithm is an important skill for anyone interested in mathematics and computer science

Some Thing to say ?