Skip to content

Resource · Guide or article

Euclidean algorithm

Introduces Euclid's GCD method through repeated subtraction, with geometric illustrations.

Reviewed

Open the original: Euclidean algorithm (opens the original site in a new tab)

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

In mathematics, the Euclidean algorithm, or Euclid's algorithm, is an efficient method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105 (as 252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 252 − 105 = 147. Since this replacement reduces the larger of the two numbers, repeating this process gives successively smaller pairs of numbers until the two numbers become equal. When that occurs, they are the GCD of the original two numbers. By reversing the steps, the GCD can be expressed as a sum of the two original numbers each multiplied by a positive or negative integer, e.g., 21 = 5 × 105 + (−2) × 252. The fact that the GCD can always be expressed in this way is known as Bézout's identity.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • Replacing the larger number by the difference keeps the GCD, shown for 252 and 105
  • Bezout's identity: 21 = 5 times 105 plus -2 times 252
  • Tiling pictures for 24 by 60 and an animation reducing 1071 by 462 to 21

How to use it

PrepKit study advice, not a claim made by the source.

Code the subtraction and remainder versions and compare step counts for 1071 and 462, then add the extended algorithm and reproduce the Bezout coefficients.

Scope and limitations

Subtraction form only: no modulo form, extended algorithm or running time.

Access and format

Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/math/euclidean-algorithm/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email