Professional Documents
Culture Documents
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.
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. */
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.
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’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.
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 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
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
Now we can observe that when we take the product gcd(a, b)*lcm(a, b), all prime factors of a
Thus,
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).
This logic for computing GCD is based on the following recursion theorem.
Examples :
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.
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,
Since a = b + (a - b) ,
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.
This logic for computing GCD is based on the following recursion theorem.
Examples :
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.
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.
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
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 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.
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).
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
Time Complexity of Step 3 =Time Complexity to list all common divisors of 'a' and '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
= Time Complexity to find the divisor which is the highest among the common divisors
= #CF(a,b)-1.
Thus
2<=#F(a)<=a
2<=#F(b)<=b
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.