AlgoMaster Logo

GCD Using Recursion

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

The greatest common divisor of two numbers is the largest integer that divides both with no remainder. For 48 and 18, the shared divisors are 1, 2, 3, 6, and the greatest is 6. Listing divisors works but is slow, and the Euclidean algorithm finds the answer far more directly.

The Euclidean algorithm rests on one fact: any number that divides both a and b also divides the remainder a % b, and the reverse holds too. So the common divisors of (a, b) are the common divisors of (b, a % b), which means gcd(a, b) = gcd(b, a % b). Replacing the pair with (b, a % b) produces a smaller pair without changing the answer, and repeating that shrinks the numbers quickly.

Key Constraints:

  • One of the inputs can be 0. The greatest common divisor of a and 0 is a, because every integer divides 0 and a divides itself. This is the base case.
  • The inputs are not both 0, so the result is always a positive integer and the problem is well defined.
  • The remainder a % b is always smaller than b, so the second number strictly decreases each step and the recursion reaches 0 quickly.

Approach 1: Recursive Euclidean

Intuition

The identity gcd(a, b) = gcd(b, a % b) is recursive on its own. To find the divisor of (a, b), find the divisor of the smaller pair (b, a % b), the same kind of problem.

The recursion stops when the second number reaches 0, where gcd(a, 0) is a, since a divides itself and also divides 0. The descent always gets there: a % b is strictly less than b, so the second argument decreases at every step until it hits 0. The first argument then holds the answer and is returned straight back up the chain.

Algorithm

  1. If b is 0, return a. This is the base case, the greatest common divisor of a and 0.
  2. Otherwise, return the result of calling the function with b and a % b.

Example Walkthrough

Input:

48
a
18
b

The first call is gcd(48, 18). Since b is not 0, it returns gcd(18, 48 % 18), which is gcd(18, 12). That returns gcd(12, 18 % 12), which is gcd(12, 6). That returns gcd(6, 12 % 6), which is gcd(6, 0). Now b is 0, so the base case returns 6. That value passes back up through every pending call, so gcd(48, 18) is 6. The pair shrank from (48, 18) to (6, 0) in three steps.

6
result

Code

Approach 2: Iterative Euclidean

Intuition

The recursion only ever carries forward the current pair, so a loop can replace it. Repeatedly set the pair (a, b) to (b, a % b) until b becomes 0, then a holds the answer. This does the same arithmetic as the recursion but reuses a single stack frame instead of nesting calls.

Algorithm

  1. While b is not 0, compute the remainder a % b, set a to b, and set b to that remainder.
  2. When b reaches 0, return a.

Example Walkthrough

Input:

48
a
18
b

The pair starts at (48, 18). The remainder 48 % 18 is 12, so the pair becomes (18, 12). Next 18 % 12 is 6, so the pair becomes (12, 6). Then 12 % 6 is 0, so the pair becomes (6, 0) and the loop stops. The answer is a, which is 6. The loop walked through the same pairs as the recursion, just without adding frames to the call stack.

6
result

Code