beginner · math · Euclidean algorithm · gcd

The largest tile that fits both walls

Problem

Two walls are a and b units long. Find the largest square tile size that covers both walls exactly with whole tiles: the greatest common divisor of a and b.

Constraints

  • 0 ≤ a, b ≤ 9999, not both 0

Examples

a = 48, b = 18 → 6

Show the solution

Euclidean algorithm

gcd(a, b) = gcd(b, a mod b): the remainder keeps every common divisor and shrinks the numbers fast.

Why it is correct

A number divides both a and b exactly when it divides both b and a mod b, so the gcd is preserved; gcd(a, 0) = a.

Complexity

Time O(log min(a, b)) iterations, extra space O(1). Every two iterations at least halve the larger number (Lamé's theorem: the worst case is consecutive Fibonacci numbers).

For contrast: Try every number from min(a, b) down to 1. That costs O(min(a, b)) time and O(1) space.

Edge cases

  • b = 0: the answer is a.
  • a < b: the first step swaps them.
  • Equal numbers.

Common mistakes

  • Repeated subtraction instead of modulo (O(a / b) iterations).
  • Negative inputs: take absolute values first.

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class GcdEuclid
{
    /// <summary>Greatest common divisor of non-negative a and b, not both 0 (Euclid). O(log min(a, b)).</summary>
    public static int Gcd(int a, int b)
    {
        while (b != 0)
        {
            int r = a % b;
            a = b;
            b = r; // every common divisor of a and b also divides r
        }
        return a;
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/GcdEuclid.cs

def gcd(a: int, b: int) -> int:
    """Greatest common divisor of non-negative a and b, not both 0 (Euclid). math.gcd is the built-in. O(log min(a, b))."""
    while b != 0:
        r = a % b
        a, b = b, r  # every common divisor of a and b also divides r
    return a

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/gcd_euclid.py

package dev.bekadan.dsa.problems;
 
public final class GcdEuclid {
    private GcdEuclid() {
    }
 
    /** Greatest common divisor of non-negative a and b, not both 0 (Euclid). O(log min(a, b)). */
    public static int gcd(int a, int b) {
        while (b != 0) {
            int r = a % b;
            a = b;
            b = r; // every common divisor of a and b also divides r
        }
        return a;
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/GcdEuclid.java

#pragma once
 
 
namespace bekadan::dsa::problems {
 
// Greatest common divisor of non-negative a and b, not both 0 (Euclid). std::gcd is the built-in. O(log min(a, b)).
inline int gcd_euclid(int a, int b) {
    while (b != 0) {
        const int r = a % b;
        a = b;
        b = r;  // every common divisor of a and b also divides r
    }
    return a;
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/gcd_euclid.hpp