You are on page 1of 12

Greatest Common Divisor - GCD

Introduction
In this report, we define the concept GCD which stands for Greatest Common Divisor
and we explain five different logics for computing the GCD value. Each logic for GCD
computation is explained with the help of examples and proof of correctness for some logics.
After the explanation of different logics, we will see asymptotic analysis of a few logics
explained. Later we will see more observations of the concept GCD obtained through our study.

Definition

The Greatest Common Divisor (GCD) of two non-negative integers a and b which are not
both 0, denoted by gcd(a,b), is the largest of the common divisors of a and b [1].

In the different logics given below for the computation of GCD value, we restrict ourselves
to nonnegative integers and later in the observations and findings section, we will see how these
logics can be used as a black box for the computation of GCD when the domain changes to
negative integers and real numbers, particularly rational numbers.

Common divisors and greatest common divisors

First let's understand the concept of divisor or factor, for that lets refer to the Division theorem.

/* Division theorem [1] : For any integer a and any positive integer n, there exist unique integers
q and r such that 0 <= r < n and a = qn + r.
The value q = floor(a/n) is the quotient of the division. The value r = a mod n is the remainder
(or residue) of the division, so that n divides a, denoted as n | a if and only if a mod n = 0.
Alternative ways of interpreting the same concept is a is divided by n or n is a factor of a or n
is a divisor of a. */

If n is a divisor of a and n is also a divisor of b , then n is a common divisor of a and b.


For example, the divisors of 30 are 1 , 2 , 3 , 5 , 6 , 10 , 15 , and 30 , and divisors of 24 are 1, 2,
3, 4, 6, 8, 12, 24. Thus the common divisors of 24 and 30 are 1 , 2 , 3 , and 6.

Some important properties of the common divisors are listed below :


1. if d | a and d | b , then d | a + b and d | a - b (1)
2. More generally, for any integers x and y, if d | a and d | b , then d | ax + by (2)
i.e. d divides any linear combination of a and b

3. if a | b , then either |a| <= |b| or b = 0 , which implies that


if a | b and b | a , then |a| = |b| (3)

Any pair of integers has a common divisor of 1 and is the least common divisor. The challenge is
of course on finding the Greatest Common Divisor (GCD), also called the Highest Common
Factor (HCF) or Greatest Common Factor (GCF).

Example : From the previous example on common divisors, we observed that the common
divisors of 24 and 30 are 1 , 2 , 3 , and 6. Now the GCD(24, 30) = 6.

Some properties GCD are listed below :


1. For any integers a and b , if d | a and d | b , then d | gcd (a, b) (4)
2. If a and b are both nonzero, then gcd (a, b) is an integer between 1 and min {|a|, |b|}.
3. gcd (a, b) = gcd (b, a)
4. gcd (a, 0) = |a|
5. gcd (a, ka) = |a|, for any k belongs to Z :

Logics for GCD computation

1. GCD using Factors

The GCD of two non-negative integers (a, b) can be determined by using divisors of a and b.
Step 1: Write the divisors of the number 'a'.
Step 2: Write the divisors of the number 'b'.
Step 3: List the common divisors of 'a' and 'b'.
Step 4: Find the divisor which is the highest among the common divisors.This divisor
will essentially be the Greatest Common Divisor(GCD).

Example: Let's find GCD (30, 24)

divisors of 30 : 1 , 2 , 3 , 5 , 6 , 10 , 15 , and 30 , and


divisors of 24 : 1, 2, 3, 4, 6, 8, 12, 24.
common divisors of 24 and 30 : 1 , 2 , 3 , and 6.
GCD (30, 24) = max {1 , 2 , 3 , 6}
2. GCD using prime factorization

It follows from the following theorem.

Unique prime factorization theorem [1]


There is exactly one way to write any composite integer ‘a’ as the product of
the form
a = p1^e1 * p2^e2 * ... * pr^er
where the pi are prime, p1<p2<...<pr , and the ei are positive integers.
As an example, the unique prime factorization of the number 6000 is:
6000 = 2^4 * 3^1 * 5^3

In principle, for positive integers a and b , their prime factorizations suffice to


compute gcd(a, b) . Indeed, if
a = p1^e1 * p2^e2 * ... * pr^er
b = p1^f1 * p2^f2 * ... * pr^fr
with 0 exponents being used to make the set of primes p1, p2, …, pn the
same for both a and b , then,
gcd(a, b) = p1^min{e1, f1} * p2^min{e2, f2} * … * pr^min{er, fr}

Example : Let’ss see how to calculate GCD (24, 30) using this logic
First let's find the prime factorizations of 24 and 30 with 0 exponents being
used to make the set of primes the same for both 24 and 30, thus
24 = 2^3 * 3^1 * 5^0
30 = 2^1 * 3^1 * 5^1
GCD(24, 30) = 2^1 * 3^1 * 5^0 = 6
The best algorithms to date for factoring do not run in polynomial time. Thus,
this approach to computing greatest common divisors seems unlikely to yield
an efficient algorithm.

3. GCD using LCM

The GCD of two positive integers (a, b) can be found from LCM (a,b) and the product of a and b
as follows :

● Step 1: Find the product of a and b.

● Step 2: Find the Least Common Multiple (LCM) of a and b.

● Step 3: Divide the product of the numbers by the LCM of the numbers.

● Step 4: The obtained value after division is the greatest common divisor of (a, b).

To find LCM (Least Common Multiplier) also we can make use of the Unique prime

factorization theorem explained before.

In principle, for positive integers a and b , their prime factorizations suffice to


compute lcm(a, b). Indeed, if
a = p1^e1 * p2^e2 * ... * pr^er
b = p1^f1 * p2^f2 * ... * pr^fr
with 0 exponents being used to make the set of primes p1, p2, …, pn the same for both a and b ,
then,
lcm(a, b) = p1^max{e1, f1} * p2^max{e2, f2} * … * pr^max{er, fr}

Example : Let’ss see how to calculate LCM (24, 30) using this logic
First let's find the prime factorizations of 24 and 30 with 0 exponents being used to make the set
of primes the same for both 24 and 30, thus
24 = 2^3 * 3^1 * 5^0
30 = 2^1 * 3^1 * 5^1
LCM(24, 30) = 2^3 * 3^1 * 5^1 = 120

Now we can have the following observation for any two positive integers a, b

a = p1^e1 * p2^e2 * ... * pr^er


b = p1^f1 * p2^f2 * ... * pr^fr
where the pi are prime, p1<p2<...<pr , and the ei are positive integers.
with 0 exponents being used to make the set of primes p1, p2, …, pn the same for both a and b ,
then,
gcd(a, b) = p1^min{e1, f1} * p2^min{e2, f2} * … * pr^min{er, fr}
lcm(a, b) = p1^max{e1, f1} * p2^max{e2, f2} * … * pr^max{er, fr}

Now we can observe that when we take the product gcd(a, b)*lcm(a, b), all prime factors of a

and b are covered, thus we get

gcd(a, b)*lcm(a, b) = a*b

Thus,

gcd(a,b) = a*b / lcm(a,b)

Hence if we have an efficient algorithm for finding out lcm(a, b), we can use it as a blackbox to

compute gcd(a,b).

GCD using Repeated Subtraction

This logic for computing GCD is based on the following recursion theorem.

GCD recursion theorem - 1

For any positive integers a and b, let a >= b then

gcd (a, b) = gcd (b, a - b)

Examples :

GCD (18, 12) = 6

GCD (12, 6) = 6

GCD (6, 6) = 6

GCD(6, 0) = 6
Proof

We can show that gcd (a, b) and gcd(b, a - b) divide each other. Since they are both positive
integers, equation (3) then implies that they must be equal.

We first show that gcd (a, b) | gcd (b, a - b).

If we let d = gcd (a, b) , then d | a and d | b.

By equation (1) , d | (a - b) .

Therefore, since d | b and d | (a - b) , Equation (4) implies that d | gcd (b, a - b) , that is,

gcd (a, b) | gcd (b, a - b)

Showing that gcd (b, a - b) | gcd (a, b) is almost the same.

If we now let d = gcd (b, a - b) , then d | b and d | (a - b).

Since a = b + (a - b) ,

By equation (1), we conclude that d | a.

Since d | b and d | a , we have that d | gcd (a, b) by equation (4) so that

gcd (b, a - b) | gcd (a, b)

This completes the proof.

The Subtraction Method is a simple and recursive method for finding the GCD of two numbers. The basic idea
is to subtract the smaller number from the larger number until both numbers become equal. The resulting
number is the GCD of the original two numbers.

Here is the step-by-step process for finding the GCD of two numbers using the Subtraction Method:

1. Take two numbers "a" and "b", where "a" is greater than or equal to "b".
2. Subtract "b" from "a". If the result is greater than or equal to "b", replace "a" with the result.
Otherwise, let "a" be the smaller number and let "b" be the difference.
3. Repeat step 2 until both numbers are equal.
4. The resulting number is the GCD of the original two numbers.

Pseudo code can be written as :


GCD (a, b)
1. if (b>a) swap a, b
2. if b == 0
3 return a
4 else return GCD (b, a - b)

5. GCD using modulo operation

This logic for computing GCD is based on the following recursion theorem.

GCD recursion theorem - 2 [1]

For any nonnegative integer a and any positive integer b ,

gcd (a, b) = gcd (b, a mod b)

Examples :

GCD (18, 12) = 6

GCD (12, 6) = 6

GCD (6, 6) = 6

Proof

We can show that gcd (a, b) and gcd(b, a mod b) divide each other. Since they are both
nonnegative, equation (3) then implies that they must be equal.

We first show that gcd (a, b) | gcd (b, a mod b).

If we let d = gcd (a, b) , then d | a and d | b.

By Division theorem, a mod b = a - qb , where q = floor (a/b).

Since (a mod b) is thus a linear combination of a and b , equation (2) implies that d | (a mod b) .
Therefore, since d | b and d | (a mod b) , Equation (4) implies that d | gcd (b, a mod b) , that is,
gcd (a, b) | gcd (b, a mod b)

Showing that gcd (b, a mod b) | gcd (a, b) is almost the same.

If we now let d = gcd (b, a mod b) , then d | b and d | (a mod b).

Since a = qb + (a mod b) , where q = floor (a/b),

we have that a is a linear combination of b and (a mod b).

By equation (2), we conclude that d | a.

Since d | b and d | a , we have that d | gcd (a, b) by equation (4) so that

gcd (b, a mod b) | gcd (a, b)

This completes the proof.

GCD using modulo operation (Euclid’s Algorithm)

Euclid’s algorithm for computing greatest common divisors relies on the above recursion
theorem. The following recursive procedure EUCLID implements Euclid’s algorithm, for finding
GCD of two arbitrary nonnegative integers a and b as input to the procedure.

EUCLID (a, b)
1. if b == 0
2 return a
3 else return EUCLID (b, a mod b)

For example, here is how the procedure computes gcd (30, 21)
EUCLID(30, 21) = EUCLID(21, 9)
= EUCLID(9, 3)
= EUCLID (3, 0) = 3

The procedure is given in a detailed manner as follows

Step 1: Divide the large number by the smaller one.

Step 2: Treat the remainder obtained as the new divisor and set the previous divisor as the new
dividend.
Step 3: Divide the first divisor by the first remainder.

Step 4: Divide the second divisor by the second remainder.

Step 5: Continue this process till the remainder becomes 0.

Step 6: The divisor, which does not leave a remainder, is the G.C.D. of the two numbers and
thus, the last divisor is the required G.C.D. of the given numbers.

Complexity Analysis
Here we will do the asymptotic time complexity analysis for the logics explained before.

1. Time Complexity of finding GCD of two numbers using Factors

The GCD of two non-negative integers (a, b) can be determined by the following procedure.
Step 1: Write the divisors of the number 'a'.
Step 2: Write the divisors of the number 'b'.
Step 3: List the common divisors of 'a' and 'b'.
Step 4: Find the divisor which is the highest among the common divisors.This divisor
will essentially be the Greatest Common Divisor(GCD).

For Time Complexity analysis we use the following notations.

F(a) : set of all factors of the number a

#F(a) : cardinality of the set F(a)

F(b) : set of all factors of the number b

#F(b) : cardinality of the set F(b)

CF(a,b): set of all common factors of the numbers a and b

#CF(a,b) : cardinality of the set CF(a,b)

Time Complexity(GCD of two numbers using Factors)


= Sum of Time Complexity of steps 1-4 of above procedure

= Time Complexity of Step 1 + Time Complexity of Step 2 + Time Complexity of Step

3 + Time Complexity of Step 4

Time Complexity of Step 1 =Time Complexity to list divisors of the number 'a' = a

[ Reason :- For a given number ‘n’, the possible divisors are coming from the set {1, 2, 3,

…, n}. Now the procedure should take each value from 1 to n and check whether it

divides n. When we divide the number n by some number x ∈ {1, 2, 3, …, n}, if we get

the remainder as 0, then we can say x is a divisor of n. Assuming constant effort for this

divisor checking, we do this repeatedly for all x ∈ {1, 2, 3, …, n} and see whether x

divides n. Hence the number of operations involved to list all divisors of the number ‘n’ =

n]

Time Complexity of Step 2 =Time Complexity to list all divisors of the number 'b' =b

[Same reasoning as step 1]

Time Complexity of Step 3 =Time Complexity to list all common divisors of 'a' and 'b'

= Number of factors of a*Number of factors of b = #F(a)*#F(b)

[Reason: Each element from the list F(a) is compared against each element of F(b) to find

the common elements, hence each element of F(a) will get #F(b) number of comparisons

and the total number of comparisons = #F(a)*#F(b)]

Time Complexity of Step 4

= Time Complexity to find the divisor which is the highest among the common divisors

= Time complexity to find the maximum from the set CF(a, b)

= #CF(a,b)-1.
Thus

Time Complexity (GCD of two numbers using factors)=a+b+[#F(a)*#F(b)]+#CF(a,b)-1

Now given two numbers ‘a’ and ‘b’

2<=#F(a)<=a

2<=#F(b)<=b

1<=#CF(a,b)<=min (#F(a), #F(b))

Worst Case Analysis[Upper bound ]

Time Complexity (GCD of two numbers using factors)=a+b+[a*b]+min(a,b)-1

BestCase Analysis[Lower bound ]

Time Complexity (GCD of two numbers using factors)=a+b+[2*2]+1

Observations and Findings


1.GCD(0,0)= NOT DEFINED , GCD(0,0) can be any number
2. GCD(0,m)=GCD(m,0)=m
3.GCD(-m,n)=GCD(m,-n)={}
GCD of one positive and one negative number does not make sense. No common factors, so empty set.
4.GCD of two negative numbers is always -1
So if we consider two negative numbers, Least Common Divisor(LCD) makes sense.
Least Common Divisor LCD(-m,-n)
a. Find GCD(m, n)
b. If GCD(m,n)=K then return -K as LCD(-m,-n)
5.GCD(np,nq)=n*GCD(p,q)

4. gcd (a, b) = gcd (-a, b)


5. gcd (a, b) = gcd (|a|, |b|)
GCD of two Rational Numbers:
A rational number is a number that is expressed as the ratio of two integers p/q where q is not zero.

Factors of a rational number:


If d is a factor of a rational number a/b then d|(a/b)
Ie. (a/b)=k(d). Where k is a positive integer.
It follows that d is any number of the form (a/b)/k
On simplification we may observe d to be of the form (Factor of a/multiple of b)
Denote
F(a)- Factor of a
m(b)- multiple of b
We observe that the number of such divisors is |N|

GCD[(a/b),(c/d)]
Divisors of a/b are of the form F(a)/m(b).
Divisors of c/d are of the form F(c)/m(d)
So the common divisors of (a/b) and (c/d) should be of the form F(a,c)/m(b,d)
The greatest of all such common divisors should have greatest of F(a,c)/ Least of m(b,d)
Ie GCD(a/b,c/d)=GCD(a,b)/LCM(b,d)

References :
Introduction to Algorithms by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein.

You might also like