← Back to list

The Euclidean Algorithm:

Find the Greatest Common Divisor (GCD) of two integers.

Abhi AIX · 2025-09-14 09:36 · 0 claps · 1.8 min read
#euclidean-algorithm #computer-science #gcd #greatest-common-divisor #programming
Open on Medium ↗
Wiki topics: 💻 · Programming 🔬 · Science · General

The Euclidean Algorithm

Find the Greatest Common Divisor (GCD) of two integers.

  • What is a GCD? The largest number that divides both of them without leaving a remainder.
  • Example: GCD(12, 18) = 6
  • The Magic Rule: GCD(a, b) = GCD(b, a mod b)
  • a mod b is the remainder when a is divided by b.

2. Why This Rule Works (The Visual Insight)

Imagine a rectangle of size a × b. The GCD is the side length of the largest square that can tile it perfectly.

Example: Find GCD(48, 18)

  1. Tile the 48×18 rectangle. The biggest square that fits is 18×18. It fits 2 times, leaving a 12×18 strip.
  2. Now, tile the leftover 18×12 strip. The biggest square is 12×12. It fits 1 time, leaving a 6×12 strip.
  3. Finally, tile the 12×6 strip. The biggest square is 6×6. It fits 2 times perfectly, leaving no remainder.

Conclusion: The side of the smallest square (6) is the GCD. The algorithm works by repeatedly breaking the problem into smaller, identical problems.

3. The Step-by-Step Algorithm

Follow this simple, efficient process. Stop when the remainder is 0.

Division EquationConclusion & Next Step48 = 2 × 18 + 12GCD(48, 18) = GCD(18, 12) → Continue18 = 1 × 12 + 6GCD(18, 12) = GCD(12, 6) → Continue12 = 2 × 6 + 0⬇️ Remainder is 0! STOP. ⬇️GCD = 6 (The last divisor)

Process Summary:

  1. Divide the larger number by the smaller number.
  2. Find the remainder.
  3. Replace the larger number with the smaller number.
  4. Replace the smaller number with the remainder.
  5. Repeat until the remainder is 0. The GCD is the last non-zero divisor.

4. Worked Example: GCD(270, 192)

Let’s apply the steps to a more complex pair.

StepDivision EquationConclusion1270 = 1 × 192 + 78GCD(270, 192) = GCD(192, 78)2192 = 2 × 78 + 36GCD(192, 78) = GCD(78, 36)378 = 2 × 36 + 6GCD(78, 36) = GCD(36, 6)436 = 6 × 6 + 0Stop. GCD = 6

∴ GCD(270, 192) = 6

5. Key Takeaways & Why It’s Efficient

  • Simple and Powerful: It transforms a difficult problem into a series of simple division steps.
  • Extremely Fast: Even for huge numbers (with hundreds of digits), the number of steps required is surprisingly small. This makes it crucial for:
  • Computer Science
  • Cryptography (e.g., RSA Encryption)
  • Simplifying Fractions
  • The Final Visual: The entire process is a journey from a large problem to a small, solved one.

“If you enjoyed this, I’d appreciate it if you shared it with someone else who might, too. Thanks for reading!” Happy Algorithms…..


메타데이터
post_id
2a7bbc192de2
slug
the-euclidean-algorithm-2a7bbc192de2
url
https://medium.com/@aibhi.dev/the-euclidean-algorithm-2a7bbc192de2
canonical_url
https://medium.com/@aibhi.dev/the-euclidean-algorithm-2a7bbc192de2
author_url
https://medium.com/@aibhi.dev
status
ok
fetched_at
2026-07-09 09:18:05