How to Find the GCD (Greatest Common Divisor)?

Last Updated : 24 Jan, 2026

The following are simple steps to find GCD of two numbers a and b.

  • Step 1: List all the divisors of the number 'a'.
  • Step 2: List all the divisors of the number 'b'.
  • Step 3: Identify the common divisors of both 'a' and 'b'.
  • Step 4: Select the largest number from the common divisors.

Let's consider an example for better understanding.

Example: Find the GCD of 13 and 48.

To solve the GCD of 13 and 48, we will first find:

  • Divisors of 13: 1, 13
  • Divisors of 48: 1, 2, 3, 4, 6, 8, 12, 16, 24, 48

The only common divisor of 13 and 48 is 1. Therefore, the GCD of 13 and 48 is 1.

So, GCD(13, 48) = 1.

There are multiple methods to find the Greatest Common Divisor (GCD) such as:

  • Prime Factorization Method
  • Euclid’s Division Algorithm
  • Binary GCD Algorithm (Stein's Algorithm)

Prime Factorization Method to Find GCD

The prime factorization method involves breaking each number down into its prime factors (prime numbers that multiply to give the original number). The GCD is found by taking the product of the lowest powers of all common prime factors.

Note: This method works only for positive numbers (natural numbers).

gcd

Example: Find the GCD of 24, 30, and 36.

Solution:

To find the GCD of 24, 30, and 36, we need to find the:

  • Prime factors of 24 = 23 × 3
  • Prime factors of 30 = 2 × 3 × 5
  • Prime factors of 36 = 22 × 32

The common prime factors are 2 and 3, and their smallest powers are 21 and 31.

So, the GCD of 24, 30, and 36 is:

GCD(24, 30, 36) = 2 × 3 = 6

Long Division Method ( Euclid’s Division Algorithm) to Find GCD

This method uses the Euclid's Division Algorithm that works for positive integers and follows these steps:

  1. Step 1: Apply Euclid’s division lemma to two numbers a and b, where a > b. The division lemma gives two numbers q (quotient) and r (remainder), such that: a = bq + r \quad \text{where} \ 0 \leq r < b
  2. Step 2: If the remainder r = 0, then b is the GCD of a and b. If r \neq 0, apply the division lemma again to b and r.
  3. Step 3: Continue the process until the remainder is zero.
  4. Step 4: When the remainder is zero, the divisor at that stage is the GCD of the given numbers.

This algorithm is efficient and works well for large numbers.

For example, we want to find GCD of 12 and 56.

GCD

GCD of 0 and a Positive Integer: GCD of a positive integer x and 0 is always x because x is the largest number that divides both x and 0. For example GCD of 0 and 5 is 5, GCD of 0 and 100 is 100.

Binary GCD Algorithm (Stein's Algorithm)

Stein's algorithm or binary GCD algorithm method uses binary operations (shifting and comparison) and is efficient for computers. In this algorithm, we can use the following steps to find GCD:

  • If both numbers are 0, the GCD is 0 (i.e., GCD(0, 0) = 0).
  • If one number is 0, the GCD is the non-zero number (i.e., GCD(a, 0) = a.
  • If both numbers are even, divide both numbers by 2: GCD(a, b) = 2 × GCD(a/2, b/2)
  • If one number is even and the other is odd, divide the even number by 2: GCD(a, b) = GCD(a/2, b)(or vice versa)
  • If both numbers are odd, subtract the smaller number from the larger one. This reduces the problem to smaller numbers: GCD(a, b) = GCD(∣a − b∣, min⁡(a, b))
  • Repeat steps 3–5 until one of the numbers becomes 0. The non-zero number at this point is the GCD.

Let's consider an example for better understanding.

Step 1: Both 18 and 24 are even, so divide both by 2 and multiply the result by 2: GCD(18, 24) = 2 × GCD(18/2, 24/2) = 2 × GCD(9, 12)

Step 2: 9 is odd and 12 is even, so divide 12 by 2: GCD(9, 12) = GCD(9, 12/2) = GCD(9, 6)

Step 3: 9 is odd and 6 is even, so divide 6 by 2: GCD(9, 6) = GCD(9, 6/2) = GCD(9, 3)

Step 4: Both 9 and 3 are odd, so subtract the smaller from the larger: GCD(9, 3) = GCD(9 − 3, 3) = GCD(6, 3)

Step 5: 6 is even, so divide by 2: GCD(6, 3) = GCD(6/2, 3) = GCD(3, 3)

Step 6: Both numbers are equal (3), so the GCD is 3.

Final Step: Multiply back the factor of 2 from Step 1: gcd⁡(18, 24) = 2 × 3 = 6

Thus, GCD(18, 24) = 6.

Read More about Program for Stein's algorithm or binary GCD.

Comment
Article Tags:

Explore