Euclidean Algorithm with Squares

Category: Number Theory   Level: 101

gcd(48, 18) = gcd(18, 12) = … = 6

Cut the biggest possible squares off a rectangle, then repeat on the leftover strip. The last square size that fits exactly is the GCD of the two sides.

Step through it

Watch it

Animated walkthrough

Step by step

Step 1: The puzzle

Find the biggest square tile that covers a 48 × 18 rectangle with no gaps and no cutting. Its side must divide both 48 and 18.

gcd(48, 18) = ?

Step 1: The puzzle

Step 2: Cut the biggest squares

Squares of side 18 are the biggest that fit. Two fit, and a 12 × 18 strip is left over. Any tile that covers the rectangle also covers that strip.

48 = 2 × 18 + 12

Step 2: Cut the biggest squares

Step 3: Repeat on the leftover strip

The strip is 12 wide, so cut squares of side 12. One fits and a 12 × 6 strip is left over.

18 = 1 × 12 + 6

Step 3: Repeat on the leftover strip

Step 4: Stop when nothing is left

Two squares of side 6 fill the last strip exactly. No leftover, so the algorithm stops.

12 = 2 × 6 + 0

Step 4: Stop when nothing is left

Step 5: Squares of side 6 tile everything

Every square in the picture is a whole number of 6 × 6 tiles, so 6 × 6 tiles cover the whole rectangle: 8 across and 3 down. No bigger tile works, so the GCD is 6.

gcd(48, 18) = 6, 8 × 3 = 24 tiles

Step 5: Squares of side 6 tile everything

All steps at once

Storyboard

Generate it yourself

cd svg-generator
python3 -m svgmath render euclidean-algorithm --out /tmp/euclidean-algorithm

Source: svgmath/diagrams/gcd_lcm.py