Module 31 of 39

Number-theoretic algorithms: gcd and modular arithmetic

Work with divisibility, gcd, modular arithmetic, repeated squaring, and the ideas behind RSA.

Some algorithms work with numbers by treating them as counts or measurements. Number-theoretic algorithms care about different properties: divisibility, remainders, primes, and the structure of integers. These ideas matter in cryptography, but they are useful long before we reach that application.

Background for this page. You need integer division and remainders. For a positive modulus n, reducing a number modulo n replaces it with its remainder. Algorithm cost for very large integers also depends on their number of bits. Related explanations: Summations for algorithm analysis · Big O, Big Omega, and Big Theta.

Two integers are congruent modulo n when they leave the same remainder after division by n. For example, 17 and 5 are congruent modulo 12. We write 17 ≡ 5 (mod 12). Modular arithmetic lets us keep intermediate values small while working with large powers: reduce after a multiplication, and the final remainder is unchanged.

Each remainder step preserves the common divisors.

The greatest common divisor, gcd(a, b), is the largest positive integer dividing both a and b. Euclid’s algorithm uses the fact that gcd(a, b) = gcd(b, a mod b) when b is not zero. Each step replaces the pair with a smaller one until the remainder is zero.

# Return the greatest common divisor.
def gcd(a, b):
    while b != 0:
        # The old b becomes the next a.
        a, b = b, a % b
    return abs(a)

For example, gcd(48, 18) becomes gcd(18, 12), then gcd(12, 6), then 6. The extended Euclidean algorithm also finds coefficients x and y with ax + by = gcd(a, b). When the gcd is 1, that relationship gives a modular inverse. It helps solve congruences such as ax ≡ b (mod n), whose solutions depend on the gcd of a and n.

The Chinese remainder theorem describes when separate remainder conditions can be combined into one condition modulo a product, most simply when the moduli are pairwise coprime. Repeated squaring computes a large modular power in O(log e) multiplications for exponent e, instead of multiplying the base e times. These are building blocks of public-key systems such as RSA, along with methods for finding and testing prime numbers.

RSA uses a public key for one operation and a private key for its inverse under carefully chosen number-theoretic conditions. A tiny classroom example can show the algebra, but it is not secure software. Real cryptography needs vetted libraries, safe padding, good randomness, correct key sizes, and protocols that handle many risks beyond the arithmetic. The course teaches the algorithmic ideas, not how to build a production cryptosystem from scratch.

Compute a modular power with repeated squaring

Find 3 to the thirteenth power modulo seven. Write 13 as 8+4+1. Repeated squaring gives 3²≡2, 3⁴≡4, and 3⁸≡2 modulo seven. Multiply the selected powers: 2·4·3=24, whose remainder is three. We computed the required remainder while keeping intermediate remainders small.

Each squaring doubles the represented exponent, so only logarithmically many multiplications are needed as the exponent grows. This counts multiplications, not individual bit operations: multiplying large integers is not constant time.

Euclid’s code returns a nonnegative gcd even for negative arguments. For inputs (0,0), it returns zero, a common programming convention; the phrase largest positive common divisor alone does not define that case.

Practice. Follow Euclid’s algorithm for 48 and 18. What is 17 modulo 12? Why can we reduce intermediate products modulo 12 when computing a large power’s remainder?

Answer. The remainders are 12, 6, then 0, so the gcd is 6. The remainder of 17 modulo 12 is 5. Replacing a number by its remainder preserves congruence modulo 12, including after multiplication.

Try another case. Does every nonzero residue have a multiplicative inverse modulo eight?

Reasoning. No. An inverse exists exactly when the value and modulus have gcd one. Two has no inverse modulo eight because all its products are even.

View the full course and choose another topic.

Image preview

Fit