Ramanujan Nagell in Lean

1 The Ramanujan–Nagell Theorem

The Ramanujan–Nagell equation is the Diophantine equation

\[ x^2 + 7 = 2^n \]

where \(x\) is an integer and \(n\) is a natural number. The theorem, conjectured by Ramanujan and proved by Nagell, states that the only solutions are \((x, n) \in \{ (\pm 1, 3),\; (\pm 3, 4),\; (\pm 5, 5),\; (\pm 11, 7),\; (\pm 181, 15)\} \).

The proof splits into two cases depending on the parity of \(n\): the even case uses a factorization argument over \(\mathbb {Z}\), while the odd case requires algebraic number theory in the ring of integers of \(\mathbb {Q}(\sqrt{-7})\).

1.1 Setup: the quadratic ring \(R = \mathbb {Z}[(1+\sqrt{-7})/2]\)

We work directly in the quadratic algebra

\[ R = \texttt{QuadraticAlgebra}\; \mathbb {Z}\; (-2)\; 1, \]

the free \(\mathbb {Z}\)-module on \(1\) and a generator \(\theta \) with \(\theta ^2 = \theta - 2\). Concretely \(R = \mathbb {Z}[(1+\sqrt{-7})/2]\), the ring of integers of \(\mathbb {Q}(\sqrt{-7})\), with \(\theta = (1+\sqrt{-7})/2\) and its conjugate \(\theta ' = (1-\sqrt{-7})/2\). In Lean these are the top-level definitions \(\theta = \langle 0, 1\rangle \) and \(\theta ' = \langle 1, -1\rangle \); there is no separate field \(K\), integral closure, or class-number computation.

Definition 1 R
✓
#

The quadratic ring \(R = \texttt{QuadraticAlgebra}\; \mathbb {Z}\; (-2)\; 1 = \mathbb {Z}[(1+\sqrt{-7})/2]\).

Proof ▶

Proof in Lean source.

Definition 2 \(\theta \)
✓
#

The generator \(\theta = \langle 0, 1\rangle = (1+\sqrt{-7})/2 \in R\).

Proof ▶

Proof in Lean source.

Definition 3 \(\theta '\)
✓
#

The conjugate \(\theta ' = \langle 1, -1\rangle = (1-\sqrt{-7})/2 \in R\).

Proof ▶

Proof in Lean source.

The following identities are all definitional (rfl) in \(R\).

Lemma 4 \(\theta ^2 = \theta - 2\)
✓
#

\(\theta ^2 = \theta - 2\).

Proof ▶

Definitional.

Lemma 5 \(\theta \cdot \theta ' = 2\)
✓
#

\(\theta \cdot \theta ' = 2\).

Proof ▶

Definitional.

Lemma 6 \(\theta + \theta ' = 1\)
✓
#

\(\theta + \theta ' = 1\).

Proof ▶

Definitional.

Lemma 7 \(\theta ' = 1 - \theta \)
✓
#

\(\theta ' = 1 - \theta \).

Proof ▶

Definitional.

Lemma 8 Norm formula
✓
#

For \(x, y \in \mathbb {Z}\), the norm of \(\langle x, y\rangle \in R\) is \(N(\langle x, y\rangle ) = x^2 + xy + 2y^2\).

Proof ▶

Unfold the quadratic-algebra norm and simplify.

Lemma 9 Norm is nonnegative
✓
#

For all \(z \in R\), \(0 \leq N(z)\). (The norm form \(x^2 + xy + 2y^2 = \tfrac 14\big((2x+y)^2 + 7y^2\big)\) is positive definite.)

Proof ▶

Complete the square: \(4N(\langle x,y\rangle ) = (2x+y)^2 + 7y^2 \geq 0\).

Lemma 10 Norm zero iff zero
✓
#

For \(z \in R\), \(N(z) = 0\) if and only if \(z = 0\).

Proof ▶

\(4N(\langle x,y\rangle ) = (2x+y)^2 + 7y^2\) vanishes only when \(x = y = 0\).

Lemma 11 Units are \(\pm 1\)
✓
#

The only units in \(R\) are \(\pm 1\).

Proof ▶

A unit has norm \(1\). From \(4N(\langle x,y\rangle ) = (2x+y)^2 + 7y^2\), the equation \(N(\langle x,y\rangle ) = 1\) forces \(y = 0\) and \(x = \pm 1\), so the unit is \(\pm 1\).

Lemma 12 Factorization of \(2\)
✓
#

In \(R\), we have \(\theta \cdot (1 - \theta ) = 2\), i.e. \(\frac{1+\sqrt{-7}}{2} \cdot \frac{1-\sqrt{-7}}{2} = 2\).

Proof ▶

Proof in Lean source.

1.1.1 \(R\) is a unique factorization domain

Rather than going through discriminants, class numbers, and Dirichlet’s unit theorem, we establish unique factorization in \(R\) directly, via the chain

\[ \texttt{EuclideanDomain}\; R \; \Longrightarrow \; \texttt{IsPrincipalIdealRing}\; R \; \Longrightarrow \; \texttt{UniqueFactorizationMonoid}\; R. \]
Theorem 13 \(R\) is a Euclidean domain
✓
#

\(R\) is a Euclidean domain, with the norm \(N\) as Euclidean function. To divide \(a\) by \(b \neq 0\) we round \(a \cdot \bar{b} / N(b)\) by a smart scheme (round the \(\sqrt{-7}\) coordinate first, then shift-round the rational part), which yields a remainder with \(16\, N(\mathrm{rem}) \leq 11\, N(b)\), hence of strictly smaller norm.

Proof ▶

Proof in Lean source.

Theorem 14 \(R\) is a principal ideal ring
✓
#

Every ideal of \(R\) is principal, since every Euclidean domain is a principal ideal ring.

Proof ▶

Proof in Lean source.

Theorem 15 \(R\) is a UFD
✓
#

\(R\) is a unique factorization domain, since every principal ideal ring is one.

Proof ▶

Proof in Lean source.

Combined with \(N(\theta ) = N(\theta ') = 2\) (so \(\theta , \theta '\) have prime norm), this gives that \(\theta \) and \(\theta '\) are irreducible, hence prime (Lemmas 54, 52, 56, 53).

1.2 The even case

When \(n\) is even, say \(n = 2k\), the equation becomes \(x^2 + 7 = 2^{2k}\), which factors over \(\mathbb {Z}\) as \((2^k + x)(2^k - x) = 7\). Since \(7\) is prime, this forces \(n = 4\) and \(x = \pm 3\).

Lemma 16 Factorization over \(\mathbb {Z}\)
✓
#

If \((2^k + x)(2^k - x) = 7\), then either \(2^k - x = 1\) and \(2^k + x = 7\), or \(2^k - x = 7\) and \(2^k + x = 1\).

Proof ▶

Both factors are positive integers whose product is the prime \(7\), so one factor is \(1\) and the other is \(7\).

1.3 The odd case

When \(n\) is odd and \(n \geq 5\), the proof works in the ring of integers of \(\mathbb {Q}(\sqrt{-7})\). Setting \(m = n - 2\), we divide the equation by \(4\) to obtain \((x^2 + 7)/4 = 2^m\), which factors in \(R\) as \(\theta ^m \cdot \theta '^{\, m}\). The conjugate factors \((x \pm \sqrt{-7})/2\) lie in \(R\) (since \(x\) is odd) and their product equals \(\theta ^m \cdot \theta '^{\, m}\). Using unique factorization and coprimality, one deduces the key identity \(-2\theta + 1 = \theta ^m - \theta '^{\, m}\).

1.3.1 Exercises: from factorization to sign condition

The proof of the main \(m\)-condition is structured as a chain of four lemmas (exercises), followed by a sign-determination step.

Lemma 17 Conjugate factors in \(R\)
✓

The conjugate factors \((x \pm \sqrt{-7})/2\) lie in \(R\) (since \(x\) is odd), and their product equals \(\theta ^m \cdot \theta '^{\, m}\). Their difference is \(2\theta - 1 = \sqrt{-7}\).

Proof ▶

Express the factors using \(\theta \) and \(\theta '\), verify the product using \(\theta \cdot \theta ' = 2\), and compute the difference.

Lemma 18 Coprimality
✓

The conjugate factors are coprime in \(R\). The only prime factors of \(2\) in \(R\) are \(\theta \) and \(\theta '\) (since \(2 = \theta \cdot \theta '\)). If either divided both factors, it would divide their difference \(\sqrt{-7}\), but \(N(\sqrt{-7}) = 7\) is not divisible by \(N(\theta ) = N(\theta ') = 2\).

Proof ▶

Norm argument: any common factor divides \(\sqrt{-7}\), whose norm is \(7\), incompatible with the norm \(2\) of \(\theta \) and \(\theta '\).

Lemma 19 UFD power association
✓

If \(\alpha \cdot \beta = \theta ^m \cdot \theta '^{\, m}\) and \(\gcd (\alpha , \beta ) = 1\) in the UFD \(R\), then \(\alpha = \pm \theta ^m\) or \(\alpha = \pm \theta '^{\, m}\). This combines unique factorization in \(R\) (UniqueFactorizationMonoid) with the fact that the only units are \(\pm 1\) (units_pm_one).

Proof ▶

Proof in Lean source.

Lemma 20 Eliminate \(x\)
✓

From \(\alpha = \pm \theta ^m\) or \(\alpha = \pm \theta '^{\, m}\), use the product relation to determine \(\beta \), then take the difference \(\alpha - \beta = 2\theta - 1\) to eliminate \(x\) and obtain: either \(2\theta - 1 = \theta ^m - \theta '^{\, m}\) or \(-2\theta + 1 = \theta ^m - \theta '^{\, m}\).

Proof ▶

Case split on the four possibilities for \(\alpha \), determine \(\beta \) from the product, and compute \(\alpha - \beta \).

Lemma 21 Must have minus sign
✓

If either \(2\theta - 1 = \theta ^m - \theta '^{\, m}\) or \(-2\theta + 1 = \theta ^m - \theta '^{\, m}\) holds for odd \(m \geq 3\), then in fact the minus sign must hold: \(-2\theta + 1 = \theta ^m - \theta '^{\, m}\). This is proved by reducing modulo \(\theta '^2\) and checking which sign is consistent.

Proof ▶

Reduce modulo \(\theta '^2\) and verify only the minus sign is consistent.

1.3.2 Key intermediate result

Lemma 22 Main \(m\)-condition
✓

For all integers \(x\) and odd \(m \geq 3\), if \((x^2 + 7)/4 = 2^m\), then

\[ -2\theta + 1 = \theta ^m - \theta '^{\, m}. \]
Proof ▶

Chain the exercises: construct the conjugate factors, prove coprimality, apply UFD association, eliminate \(x\), then determine the sign.

1.3.3 From the \(m\)-condition to finitely many solutions

Lemma 23 Reduction by dividing by \(4\)
✓
#

If \(n\) is odd with \(n \geq 5\) and \(x^2 + 7 = 2^n\), then \((x^2 + 7)/4 = 2^{n-2}\).

Proof ▶

Since \(n \geq 5\), we have \(4 \mid 2^n\), so divide both sides by \(4\).

Lemma 24 Binomial expansion mod \(7\)
✓

From \(-2\theta + 1 = \theta ^m - \theta '^{\, m}\), expand via the binomial theorem and reduce modulo \(7\) to obtain \(-2^{m-1} \equiv m \pmod{7}\).

The proof multiplies both sides by \(2^m\), expands \((1 + \sqrt{-7})^m - (1 - \sqrt{-7})^m\) via the binomial theorem, observes that even-index terms cancel and odd-index terms involve powers of \((\sqrt{-7})^2 = -7\), then reads the result modulo \(7\).

Proof ▶

Binomial expansion and reduction modulo \(7\).

Lemma 25 Mod \(7\) constraint
✓
#

If \(n\) is odd with \(n \geq 5\) and \(x^2 + 7 = 2^n\), then \((-2)^{n-3} \equiv n - 2 \pmod{7}\).

This follows from the \(m\)-condition (Lemma 22) by expanding \(\theta ^m - \theta '^{\, m}\) via the binomial theorem and reducing modulo \(7\).

Proof ▶

Combine the reduction and binomial expansion lemmas.

Theorem 26 Mod \(42\) constraint
✓
#

If \(n\) is odd with \(n \geq 5\) and \(x^2 + 7 = 2^n\), then \((n - 2) \bmod 42 \in \{ 3, 5, 13\} \).

This follows from \((-2)^{n-3} \equiv n - 2 \pmod{7}\) together with Fermat’s little theorem \(2^6 \equiv 1 \pmod{7}\). Checking residues modulo \(42\) (combining mod \(6\) and mod \(7\)) yields the three residue classes.

Proof ▶

Exhaustive check of residues modulo \(42\) using the mod \(7\) constraint and Fermat’s little theorem.

1.3.4 Uniqueness per residue class

The mod \(42\) constraint narrows \(m = n - 2\) to three residue classes. To show that each class contains at most one solution, we use a \(7\)-adic argument.

Definition 27 Binomial sum \(B_d\)
✓
#

Define the odd-indexed binomial sum

\[ B_d = \sum _{j=0}^{(d-1)/2} \binom {d}{2j+1} \cdot (-7)^j. \]

This arises from expanding \((1+\sqrt{-7})^d = A_d + \sqrt{-7} \cdot B_d\).

Proof ▶

Proof in Lean source.

Lemma 28 7-adic valuation of \(B_d\)
✓
#

The \(7\)-adic valuation of \(B_d\) equals \(v_7(d)\): if \(7^l \| d\) (i.e. \(7^l \mid d\) but \(7^{l+1} \nmid d\)), then \(7^l \mid B_d\) and \(7^{l+1} \nmid B_d\).

This is the core of the \(7\)-adic analysis: the \(j=0\) term of \(B_d\) equals \(d\), and all higher terms have strictly larger \(7\)-adic valuation.

Proof ▶

The \(j=0\) term is \(d\) with valuation \(l\). Each \(j \geq 1\) term has \(7\)-adic valuation \(\geq l+1\), so by the ultrametric property the sum has valuation exactly \(l\).

Definition 29 Even-indexed binomial sum \(A'_d\)
✓
#

Define the even-indexed binomial sum

\[ A'_d = \sum _{j=0}^{d/2-1} \binom {d}{2(j+1)} \cdot (-7)^j. \]

This arises from the even-index part of the expansion of \((1+\sqrt{-7})^d\).

Proof ▶

Proof in Lean source.

Lemma 30 7-adic valuation of \(A'_d\)
✓
#

If \(7^l \| d\) and \(7 \mid d\), then \(7^l \mid A'_d\) and \(7^{l+1} \nmid A'_d\). The \(j=0\) term \(\binom {d}{2} = d(d-1)/2\) has valuation \(l\), and all higher terms have strictly larger \(7\)-adic valuation.

Proof ▶

Same structure as for \(B_d\): the leading term \(\binom {d}{2}\) has valuation \(l\), and higher terms are absorbed.

Definition 31 Trace sequence
✓
#

Define the integer recurrence \(a(0) = 2\), \(a(1) = 1\), \(a(n+2) = a(n+1) - 2\, a(n)\). This is the trace sequence: \(a(n) = \theta ^n + \theta '^{\, n}\) in \(R\).

Proof ▶

Proof in Lean source.

Lemma 32 Trace sequence equals \(\theta ^n + \theta '^{\, n}\)
✓

For all \(n\), \(\mathrm{trace\_ seq}(n) = \theta ^n + \theta '^{\, n}\) in \(R\).

Proof ▶

Induction on \(n\) using the recurrence, with \(\theta + \theta ' = 1\) and \(\theta \cdot \theta ' = 2\).

Lemma 33 Trace sequence not divisible by \(7\)
✓
#

For all \(n\), \(7 \nmid a(n)\). The recurrence has period \(3\) modulo \(7\), and the three residues \(a(0) \equiv 2\), \(a(1) \equiv 1\), \(a(2) \equiv -3\) are all nonzero mod \(7\).

Proof ▶

Show the recurrence is periodic mod \(7\) with period \(3\), then check all three residues.

Lemma 34 Even iff not odd
✓
#

For natural numbers, \(n\) is even if and only if \(n\) is not odd.

Proof ▶

Immediate from not_odd_iff_even.

Lemma 35 At most one solution per residue class
✓

If \(m_1, m_2\) are both odd, \(\geq 3\), satisfy \(m_1 \equiv m_2 \pmod{42}\), and both give \(-2\theta + 1 = \theta ^{m_i} - \theta '^{\, m_i}\), then \(m_1 = m_2\).

Proof sketch: if \(m_1 \neq m_2\), let \(d = |m_2 - m_1|\), which is divisible by \(42\) (hence by \(7\)). The \(7\)-adic analysis of Lemma 28 combined with Corollary C yields a contradiction on the valuation of \(\sqrt{-7} \cdot B_d\).

Proof ▶

Contradiction via \(7\)-adic valuation: \(d\) is divisible by \(42\) (hence \(7\)), and the valuation identity forces an impossible parity.

1.3.5 Verification of known solutions

Lemma 36 Verification: \(m = 3\) (i.e. \(n = 5\))
✓
#

\(-2\theta + 1 = \theta ^3 - \theta '^{\, 3}\). Verified via \(x = 5\): \((25 + 7)/4 = 8 = 2^3\).

Proof ▶

Direct computation using \(\theta ^2 = \theta - 2\).

Lemma 37 Verification: \(m = 5\) (i.e. \(n = 7\))
✓
#

\(-2\theta + 1 = \theta ^5 - \theta '^{\, 5}\). Verified via \(x = 11\): \((121 + 7)/4 = 32 = 2^5\).

Proof ▶

Direct computation using \(\theta ^2 = \theta - 2\).

Lemma 38 Verification: \(m = 13\) (i.e. \(n = 15\))
✓
#

\(-2\theta + 1 = \theta ^{13} - \theta '^{\, 13}\). Verified via \(x = 181\): \((32761 + 7)/4 = 8192 = 2^{13}\).

Proof ▶

Direct computation using \(\theta ^2 = \theta - 2\).

1.3.6 Combining

Theorem 39 Odd case: only three values
✓

If \(n\) is odd with \(n \geq 5\) and \(x^2 + 7 = 2^n\), then \(n \in \{ 5, 7, 15\} \).

From the mod \(42\) constraint, \(m = n - 2\) lies in one of three residue classes (\(3\), \(5\), or \(13\) mod \(42\)). The verification lemmas show these are actual solutions (at \(m = 3, 5, 13\)). The uniqueness lemma shows each residue class has at most one solution. Therefore \(n \in \{ 5, 7, 15\} \).

Proof ▶

Combine the mod \(42\) constraint, uniqueness per class, and verification of the three known solutions.

1.4 Main theorem

1.4.1 Direct computation

Once the possible values of \(n\) are determined (either by the even-case factorization or the odd-case modular argument), it remains to solve for \(x\) by direct computation. A single generic lemma handles all five cases: it takes an equation \(x^2 = a^2\) (where \(a^2 = 2^n - 7\)) and the value of \(n\), and identifies the solution pair \((x, n)\).

Lemma 40 Solving \(x^2 = a^2\)
✓
#

If \(x^2 = a^2\) and \(n = m\), then \((x, n) = (a, m)\) or \((x, n) = (-a, m)\).

Proof ▶

From \(x^2 = a^2\) we get \(x = a\) or \(x = -a\).

1.4.2 The theorem

Theorem 41 Ramanujan–Nagell
✓
#

The only integer solutions to \(x^2 + 7 = 2^n\) are

\[ (x, n) \in \{ (\pm 1, 3),\; (\pm 3, 4),\; (\pm 5, 5),\; (\pm 11, 7),\; (\pm 181, 15)\} . \]
Proof ▶

First, one shows \(n \geq 3\) by bounding \(2^n \geq x^2 + 7 \geq 7\).

Case 1: \(n\) even. Write \(n = 2k\). Then \((2^k+x)(2^k-x) = 7\). By Lemma 16, the only possibility is \(2^k = 4\), giving \(n = 4\) and \(x = \pm 3\) (Lemma 40).

Case 2: \(n\) odd, \(n = 3\). Direct computation gives \(x^2 = 1\), so \(x = \pm 1\) (Lemma 40).

Case 3: \(n\) odd, \(n \geq 5\). By Theorem 39, \(n \in \{ 5, 7, 15\} \), and direct computation gives the remaining solutions (Lemma 40).

1.5 Additional declarations

Lemma 42 associated_eq_or_neg
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 43 associated_of_theta_pow_dvd
✓

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 44 associated_of_theta_pow_dvd_right
✓

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 45 factor_not_unit_left
✓

The conjugate factors ‘(x ± √-7)/2‘ lie in ‘R‘ (since ‘x‘ is odd) and

Proof ▶

Proof in Lean source.

Lemma 46 factor_not_unit_right
✓

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 47 four_norm_eq
✓
#

The integer ring ‘ℤ[(1 + √-7)/2]‘, packaged as ‘QuadraticAlgebra ℤ (-2) 1‘. -/

Proof ▶

Proof in Lean source.

Lemma 48 norm_pos
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Definition 49 quot
✓
#

The integer ring ‘ℤ[(1 + √-7)/2]‘, packaged as ‘QuadraticAlgebra ℤ (-2) 1‘. -/

Proof ▶

Proof in Lean source.

Lemma 50 quot_mul_add_rem_eq
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Definition 51 rem
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 52 theta’_irreducible
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 53 theta’_prime
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 54 theta_irreducible
✓
#

TODO: add description

Proof ▶

Proof in Lean source.

Lemma 55 theta_pow_dvd_of_coprime_prod
✓

The integer ring ‘ℤ[(1 + √-7)/2]‘, packaged as ‘QuadraticAlgebra ℤ (-2) 1‘. -/

Proof ▶

Proof in Lean source.

Lemma 56 theta_prime
✓
#

The integer ring ‘ℤ[(1 + √-7)/2]‘, packaged as ‘QuadraticAlgebra ℤ (-2) 1‘. -/

Proof ▶

Proof in Lean source.

Lemma 57 ufd_associated_dichotomy
✓

TODO: add description

Proof ▶

Proof in Lean source.