Why it matters

Number theory algorithms unlock cryptographic understanding and competitive programming problems.

Advertisement

The architecture

Euclidean GCD: gcd(a, b) = gcd(b, a mod b). O(log min(a,b)).

Extended Euclidean: also compute Bezout coefficients (x, y such that ax + by = gcd(a, b)).

Number theory basicsGCDEuclideanExtendedBezout coefsModular inversevia extended or FermatModular inverse a^-1 mod m exists iff gcd(a, m) = 1
Number theory operations.
Advertisement

How it works end to end

Modular exponentiation: a^n mod m via squaring. O(log n).

Modular inverse: a^-1 mod m. Via extended Euclidean or Fermat's Little Theorem (a^(p-1) ≡ 1 mod p for prime p).

Chinese Remainder Theorem: combine multiple modular equations.

Applications: RSA cryptography, competitive programming, combinatorics with modular arithmetic.