Math Core

Module 3.4 · Number Theory

Diophantine equations

A Diophantine equation is an equation where only integer solutions count. There is no single method for all of them, but AIME problems almost always yield to one of a handful of moves: solve a linear equation with Bézout, factor (often with Simon's Favorite Factoring Trick), parametrize Pythagorean triples, rule out cases with a modulus, or recognize a Pell equation. This module teaches each move and when to reach for it.

Linear equations

Linear Diophantine equations

ax+by=cax + by = c has integer solutions if and only if g=gcd⁡(a,b)g = \gcd(a, b) divides cc. If (x0,y0)(x_0, y_0) is one solution, all solutions are

x=x0+bgt,y=y0−agt,t∈Z.x = x_0 + \frac{b}{g}t, \qquad y = y_0 - \frac{a}{g}t, \qquad t \in \mathbb{Z}.

Why. Bézout's identity says g=au+bvg = au + bv for some integers u,vu, v (the Euclidean algorithm run backward finds them), so if g∣cg \mid c you can scale up to hit cc. Conversely gg divides the left side, so it must divide cc. For the general solution: if ax+by=ax0+by0ax + by = ax_0 + by_0, then a(x−x0)=b(y0−y)a(x - x_0) = b(y_0 - y). Dividing by gg, ag(x−x0)=bg(y0−y)\frac{a}{g}(x - x_0) = \frac{b}{g}(y_0 - y) with ag,bg\frac ag, \frac bg coprime, so bg∣x−x0\frac bg \mid x - x_0.

To count positive solutions, find one solution, write the general one, and count the values of tt that keep both variables in range.

Chicken McNugget theorem

The Frobenius number for two coins

If a,ba, b are coprime positive integers, the largest integer that is not of the form ax+byax + by with x,y≥0x, y \ge 0 is ab−a−bab - a - b. Exactly (a−1)(b−1)2\dfrac{(a-1)(b-1)}{2} positive integers are not representable.

Why. Any nn can be written n=ax+byn = ax + by with a unique y∈{0,1,…,a−1}y \in \{0, 1, \dots, a-1\} (choose yy with by≡n(moda)by \equiv n \pmod a). Then nn is representable with nonnegative coefficients exactly when this xx is ≥0\ge 0, that is, n≥byn \ge by. The largest failure takes y=a−1y = a - 1 and x=−1x = -1: n=b(a−1)−a=ab−a−bn = b(a-1) - a = ab - a - b. For the count, nn is representable iff ab−a−b−nab - a - b - n is not (for 0≤n≤ab−a−b0 \le n \le ab - a - b), so exactly half of those ab−a−b+1=(a−1)(b−1)ab - a - b + 1 = (a-1)(b-1) numbers fail.

Factoring

If an equation has a product xyxy and linear terms, complete the rectangle:

xy+ax+by=c  ⟺  (x+b)(y+a)=c+ab.xy + ax + by = c \iff (x + b)(y + a) = c + ab.

This is Simon's Favorite Factoring Trick. The right side is a fixed number, so solutions correspond to its factor pairs. Count positive divisors and then check which factor pairs keep x,yx, y in range, including negative factor pairs.

The same idea handles differences of squares, m2−n2=(m−n)(m+n)m^2 - n^2 = (m - n)(m + n), where the two factors must have the same parity.

Worked example: Factoring trick

Find all positive integer solutions of xy+2x+3y=30xy + 2x + 3y = 30.

Add 66: (x+3)(y+2)=36(x + 3)(y + 2) = 36. With x≥1x \ge 1, y≥1y \ge 1 we need x+3≥4x + 3 \ge 4 and y+2≥3y + 2 \ge 3. Factor pairs of 3636: (4,9),(6,6),(9,4),(12,3)(4, 9), (6, 6), (9, 4), (12, 3) work, while (18,2)(18, 2) and (36,1)(36, 1) make y≤0y \le 0. The solutions are (x,y)=(1,7),(3,4),(6,2),(9,1)(x, y) = (1, 7), (3, 4), (6, 2), (9, 1).

Pythagorean triples

All Pythagorean triples

Every primitive triple (gcd⁡=1\gcd = 1) with a2+b2=c2a^2 + b^2 = c^2 and bb even has the form

a=m2−n2,b=2mn,c=m2+n2a = m^2 - n^2, \qquad b = 2mn, \qquad c = m^2 + n^2

with m>n≥1m > n \ge 1, gcd⁡(m,n)=1\gcd(m, n) = 1 and m−nm - n odd. Every triple is kk times a primitive one.

Sketch. In a primitive triple exactly one leg is even; say bb. Then b2=(c−a)(c+a)b^2 = (c - a)(c + a), and c−a2,c+a2\frac{c-a}{2}, \frac{c+a}{2} are coprime integers whose product is the square (b2)2\left(\frac b2\right)^2. Coprime numbers whose product is a square are both squares: c+a2=m2\frac{c+a}{2} = m^2, c−a2=n2\frac{c-a}{2} = n^2.

Often you don't even need the formula: with a known leg aa, write a2=(c−b)(c+b)a^2 = (c - b)(c + b) and count factor pairs of the same parity.

Worked example: Fixed perimeter

Find all right triangles with integer sides and perimeter 6060.

The perimeter of k(m2−n2,2mn,m2+n2)k(m^2 - n^2, 2mn, m^2 + n^2) is 2km(m+n)2km(m+n), so km(m+n)=30km(m + n) = 30 with m<m+n<2mm < m + n < 2m. Try (m,n)=(2,1)(m, n) = (2, 1): m(m+n)=6m(m+n) = 6, k=5k = 5, giving (15,20,25)(15, 20, 25). Try (3,2)(3, 2): 1515, k=2k = 2, giving (10,24,26)(10, 24, 26). Other pairs have m(m+n)=12,20,28,30,…m(m + n) = 12, 20, 28, 30, \dots that either don't divide 3030 or break the conditions ((5,1)(5, 1) has m−nm - n even). So there are exactly two triangles.

Ruling out solutions with a modulus

To show an equation has no solutions, reduce it modulo something where squares (or cubes, or powers) take few values. Good moduli: 44 and 88 for squares (x2≡0,1,4(mod8)x^2 \equiv 0, 1, 4 \pmod 8), 99 or 77 for cubes, 33, 55, 1616.

Worked example: No solutions

Show that x2−3y2=2026x^2 - 3y^2 = 2026 has no integer solutions.

Mod 44: x2−3y2≡x2+y2x^2 - 3y^2 \equiv x^2 + y^2, and 2026≡22026 \equiv 2, so xx and yy are both odd. Then mod 88: x2≡y2≡1x^2 \equiv y^2 \equiv 1, so x2−3y2≡−2≡6x^2 - 3y^2 \equiv -2 \equiv 6. But 2026≡2(mod8)2026 \equiv 2 \pmod 8. Contradiction.

Pell equations

For a non-square DD, the equation x2−Dy2=1x^2 - Dy^2 = 1 has infinitely many positive solutions. If (x1,y1)(x_1, y_1) is the smallest, all of them come from

xk+ykD=(x1+y1D)k.x_k + y_k\sqrt D = \left(x_1 + y_1\sqrt D\right)^k.

The reason: (x2−Dy2)(x^2 - Dy^2) is multiplicative in the sense that the "norm" of a product equals the product of norms, so powers of a solution are solutions. (That these are all the solutions takes more work; you can quote it.) For x2−2y2=1x^2 - 2y^2 = 1, starting from 3+223 + 2\sqrt2: (3+22)2=17+122(3 + 2\sqrt2)^2 = 17 + 12\sqrt2, then 99+70299 + 70\sqrt2, and so on.

Common mistake

When counting factor pairs, don't forget negative pairs and don't forget parity. For (x−6)(y−4)=N(x - 6)(y - 4) = N, both factors could be negative; check whether that's consistent with x,y≥1x, y \ge 1. For m2−n2=Nm^2 - n^2 = N, the factors m−nm - n and m+nm + n must both be even or both odd.

Practice

Practice 1

How many ordered pairs of positive integers (x,y)(x, y) satisfy 7x+11y=10007x + 11y = 1000?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

A store sells stickers only in packs of 1717 and packs of 2323. How many positive integers nn are not the total number of stickers in some purchase?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

How many ordered pairs of positive integers (x,y)(x, y) satisfy xy−4x−6y=2026xy - 4x - 6y = 2026?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

How many ordered pairs of positive integers (x,y)(x, y) satisfy 1x+1y=160\dfrac{1}{x} + \dfrac{1}{y} = \dfrac{1}{60}?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

How many right triangles with integer side lengths have a leg of length 4848? (Triangles that are congruent count once.)

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

How many ordered triples (x,y,z)(x, y, z) of nonnegative integers satisfy 3x+5y+7z=1003x + 5y + 7z = 100?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Find the sum of all positive integers nn such that n2+2024n^2 + 2024 is a perfect square.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

Find the largest positive integer n<1000n < 1000 such that 1+2+⋯+n1 + 2 + \cdots + n is a perfect square.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice

  • 2015 AIME I, Problem 3: a prime pp with 16p+116p + 1 a perfect cube; factor n3−1n^3 - 1.
  • 1999 AIME, Problem 3: when a quadratic in nn is a perfect square; complete the square and factor a difference of squares.