Skip to content
uencoder

GCD

The GCD (greatest common divisor) is the largest positive integer that divides two or more integers, computed efficiently with the Euclidean algorithm.

Purpose

To find the largest common factor of integers, used in fraction reduction and number theory.

Mechanism

Euclidean algorithm: gcd(a,b) = gcd(b, a mod b), until b=0. The extended Euclidean algorithm additionally finds Bezout coefficients x,y with ax+by=gcd(a,b), used for modular inverses. lcm(a,b) = |a×b|/gcd(a,b).

Use cases

  • Fraction reduction
  • Modular inverse computation
  • RSA key validation
  • Rational arithmetic

Examples

  • Input
    gcd(48, 18)
    Output
    6

    48=6×8, 18=6×3

Common errors

gcd with 0 left unhandled
gcd(a,0)=|a|, gcd(0,0)=0

Related terms

Related tools

References