Skip to content
StackDecode
Number theory & mathAnimated

GCD

The largest number that divides both inputs.

Time

O(log min(a, b))

Extra space

O(1)

Use it for

Simplifying fractions.

How it works

  1. The GCD is the largest number that divides both inputs without a remainder.
  2. Replace (a, b) by (b, a mod b) until b is 0.
  3. The remaining a is the GCD.

Watch it run

Loading animation…