One Number Against Fermat
Fermat was almost sure that every number 2^(2^n) + 1 is prime. The first five are. Euler found the number that settles it, and we check his answer with two proof checkers, a computation and a set of mistakes they must catch.
The question
In 1640 Pierre de Fermat wrote to his fellow number theorist Frénicle about a family of numbers he had been testing:
$$F_n = 2^{2^n} + 1.$$He was, he said, almost convinced that every one of them is prime. It is easy to see why. Here are the first five:
| $n$ | $F_n$ | |
|---|---|---|
| 0 | 3 | prime |
| 1 | 5 | prime |
| 2 | 17 | prime |
| 3 | 257 | prime |
| 4 | 65537 | prime |
Five out of five. There is even a reason to expect something special of these numbers: $2^m + 1$ can be prime only when $m$ is a power of two, because if $m$ has an odd factor $q > 1$ then $2^{m/q} + 1$ divides it (for example, $2^{12} + 1 = 4097$ is divisible by $2^4 + 1 = 17$). The numbers $F_n$ are exactly the candidates that survive that test.
The sixth number is
$$F_5 = 2^{32} + 1 = 4294967297.$$Is it prime? Fermat could not tell. Checking by hand means dividing by every prime up to its square root, which is just over 65536: thousands of long divisions with ten-digit numbers.
The setup
In 1732 Leonhard Euler announced that $F_5$ is divisible by 641:
$$2^{32} + 1 = 4294967297 = 641 \times 6700417.$$That single line settles the question. It is a complete proof, and you can check it with a calculator, or by hand in a few minutes. Since $1 < 641 < 4294967297$, the number has a divisor other than 1 and itself, so it is not prime. Fermat’s claim is about every $n$, and it fails at $n = 5$.
Five successes before a failure can reasonably have raised Fermat’s confidence: examples are evidence, and a mathematician who ignored them would be a poor one. What examples cannot do is establish a statement about every $n$. One failure is enough to refute it, and no number of successes is enough to prove it. The Fermat numbers are pairwise coprime: $F_0F_1\cdots F_{m-1}=F_m-2$, so a common divisor of two would divide $2$. The first five successes were therefore five separate cases, not five views of one fact — and still not a proof.
Euler’s 1732 paper does not say how he found 641. Exercise 3 below shows why it was a natural number to try.
The words “almost convinced” (quasi persuadé) are quoted here from secondary sources. Fermat’s letter is in his collected works (Œuvres de Fermat, vol. II, 1894); the letter itself has not been transcribed here.
Run it
The multiplication needs no software. So why run any? Because the method is the point. Later investigations in this book have answers that are not one line long, and there we will need to know exactly what a machine check is worth. Fermat is the case where you can see everything with your own eyes, so it is the place to learn what to look for.
The investigation runs three checks that share no code:
- Lean, a proof checker, with its mathematics library Mathlib, checks a proof of “not every $2^{2^n} + 1$ is prime”, along with the factorization, the divisor, the five true cases and the fact that 6700417 is prime.
- Rocq, a second proof checker with a different design, checks the factorization, the divisor and the same refutation.
- Python with SymPy recomputes everything by exact integer arithmetic.
In both proof checkers the arithmetic is checked inside the proof assistant:
Lean evaluates the closed computation in its kernel, and Rocq’s vm_compute
produces a proof term that the kernel then checks. Neither accepts an outside
program’s answer.
In the pinned environment (see “Running the investigations” in the introduction to Part I):
math-engine proof fermat
Without Docker, the independent computation runs with plain Python:
python -m math_engine.proofs.fermat_compute --divisor 641 --cofactor 6700417.
The recorded run is in
experiments/investigations/n1-fermat/kernel-runs/2026-11-13-approved/;
start with its notebook.md.
What happened
The recorded run printed:
Lean + Mathlib 7/7 statements accepted as written in the contract [passed]
Rocq 4/4 statements accepted as written in the contract [passed]
Python + SymPy (exact integer arithmetic): refuted [passed]
Failure controls 9/9 rejected as expected
It also wrote a notebook with the full record.
The run also made nine deliberate mistakes of five kinds, the failure controls, and each had to be rejected at the place that was changed:
| The mistake | Why it must fail |
|---|---|
| 641 replaced by 643 | 643 does not divide $F_5$; the remainder is 572 |
| 6700417 replaced by 6700419 | the product is then 4294968579 |
| Euler’s divisor used at $n = 4$ | $F_4 = 65537$ is prime, so 641 cannot divide it |
| the proof attached to $2^{2^n} + 3$ | the proof is about $+1$, and must not be credited to any other statement |
| the computation given 643 | it must report a non-divisor |
The first four kinds are instantiated in both Lean and Rocq, giving eight kernel mutations; the computation control is the ninth. All nine were rejected, each where it was made.
Why
Euler’s line is the whole proof: a number with a divisor strictly between 1 and itself is not prime, and $641 \times 6700417$ is $2^{32} + 1$. Exercise 2 shows how to check the divisibility with no long division at all.
Why 641, of all numbers? Every prime divisor of $F_5$ must have the form $128k + 1$, and 641 is the first prime of that form that divides it; exercise 3 walks through the search. (The $128k + 1$ form is a later theorem, not something Euler stated.)
The scale is what makes the witness non-obvious: the first five Fermat numbers fit in a few digits, while $F_5$ is ten digits and its trial divisors run into the tens of thousands.
Fermat numbers $F_0$ to $F_5$ on a log scale. The first five are prime; $F_5$ is composite, $641 \times 6700417$. The dashed line marks how far trial division must reach ($\sqrt{F_5} \approx 65{,}536$). The figure shows magnitudes only; primality of $F_0$–$F_4$ is checked by the kernels, not by the picture.
What was actually checked
A proof checker certifies that a proof proves its own statement. Whether that statement is the claim we care about is a separate question, and it is easy to get wrong: a theorem can carry a reassuring name and say something weaker, as Chapter 5 shows.
So the claim is written down first, in a claim contract, before any proof runs. The contract gives the mathematical statement, how it is read, and the exact text of each formal statement. The run then asks each checker to confirm that the theorem it accepted has exactly the statement in the contract, not merely the same name. The fourth mistake above tests this step.
Lean states the claim over the natural numbers with Mathlib’s definition of prime. Rocq states it over the integers with its own definition. The two are meant to express the same mathematical claim, and three short bridges in the contract say why: the two definitions of prime agree, the arithmetic agrees, and divisibility agrees. The bridges are argued, not kernel-checked.
The formal statements and the three bridges are a reading of the claim, not a machine result. That reading is recorded against the exact version of the contract, and it is a separate judgement from the kernel checks. It does not cover the wording of Fermat’s letter, which has not been transcribed.
What this establishes
Fermat’s claim that every $2^{2^n} + 1$ is prime is false. The elementary refutation, $2^{32} + 1 = 641 \times 6700417$, is complete by itself. Two proof checkers and an independent computation confirm it, and each of nine deliberate mistakes was caught where it was made. The formal statements are tied to the prose claim by the contract’s bridges.
What remains open
The result does not settle the primality of any $F_n$ for $n > 5$:
- Every $F_n$ from $n = 5$ to $n = 32$ is known to be composite. This is published work, not checked here.
- Nobody knows whether $F_{33}$ is prime.
- Nobody knows whether there is any prime $F_n$ beyond 65537.
It does not tell us how Euler found 641. Fermat’s own words have not been transcribed from the primary source, so the quotation and attribution rest on secondary sources. The formal statements are tied to the prose claim by the contract’s bridges; that reading is a separate judgement from the kernel checks.
Try it yourself
-
Run the investigation and open the notebook it writes. Then check the run directory with
math-engine proof fermat --verify <dir>, change one character innotebook.md, and check it again: the verification names the file. -
Euler’s divisor by hand. Check that $641 = 5 \cdot 2^7 + 1$ and also $641 = 5^4 + 2^4$. Use these to show that $641$ divides $2^{32} + 1$ without any long division. Answer. Work modulo 641. From the first form, $5 \cdot 2^7 \equiv -1$, so $5^4 \cdot 2^{28} = (5 \cdot 2^7)^4 \equiv 1$. From the second, $2^4 \equiv -5^4$, so $2^{32} = 2^4 \cdot 2^{28} \equiv -5^4 \cdot 2^{28} \equiv -1$. That is, $641 \mid 2^{32} + 1$.
-
Why 641 is a natural candidate. Check that $641 = 128 \cdot 5 + 1$. A later theorem says that every prime divisor of $F_5$ has the form $128k + 1$. Which values of $k$ below 5 give primes, and do any of them divide $F_5$? Answer. $k = 1, 2, 3, 4$ give 129, 257, 385 and 513. Only 257 is prime, and $F_5$ leaves remainder 2 on division by 257, so it is not a divisor. The next candidate, $k = 5$, is 641.
-
Edit the contract’s statement for
fermat_claim_false(inexperiments/investigations/n1-fermat/investigation.json) to say something slightly different, and run the investigation again. What does the summary say, and why is that the right answer?
Worked answers: Chapter 35 — Appendix A: Solutions to the Exercises (Part IV, Appendix A)#chapter-1-one-number-against-fermat.
Evidence record
The complete technical record for this run is in its notebook and
result.json: every statement checked, the type fingerprints, the axioms, the
controls and the hashes of every log. The summary:
| Investigation | N1-FERMAT-01, claim contract version 1 (sha256 ef384bbf…) |
| Run | 20261113T161233Z-9c5ae090 from commit 2e6f2d4, on the pinned math-env image (Lean 4.35.0 + Mathlib, Rocq 9.0.1) |
| Result | result.json sha256 f76130c9… |
| What it establishes | The kernels accepted the formal statements; the factorization and divisibility are also checked by exact computation. The bridges between those statements and the prose claim are argued, not kernel-checked. |
| Record | experiments/investigations/n1-fermat/kernel-runs/2026-11-13-approved/ (earlier executions are preserved alongside it) |
Sources
- L. Euler, “Observationes de theoremate quodam Fermatiano aliisque ad numeros primos spectantibus”, Commentarii academiae scientiarum Petropolitanae 6 (1738), 103–107; presented 26 September 1732. E26 in the Eneström index.
- J. Bell (translator), “Observations on a certain theorem of Fermat and on others concerning prime numbers”, arXiv:math/0501118.
- P. de Fermat, letter to Frénicle, 1640, in P. Tannery and C. Henry (eds), Œuvres de Fermat, vol. II (1894). Not transcribed here.
- The divisor form $128k + 1$ in exercise 3: R. P. Brent, “Factorization of the tenth Fermat number”, Mathematics of Computation 68 (1999), 429–451, §1, which states that for $n \ge 2$ every factor of $F_n$ has the form $k \cdot 2^{n+2} + 1$. The result is due to É. Lucas, American Journal of Mathematics 1 (1878); that primary source has not been read here.
- Repository:
experiments/investigations/n1-fermat/investigation.json,formal/lean/FermatEuler.lean,formal/rocq/FermatEuler.v.
Further Reading
For deeper study; not sources for the chapter’s claims.
- Michal Křížek, Florian Luca, Lawrence Somer. 17 Lectures on Fermat Numbers: From Number Theory to Geometry (Springer, 2001). DOI. Why read it: A book-length modern treatment of Fermat numbers, their divisors, primality tests and factorization, well beyond the single counterexample in the chapter.
- Richard P. Brent, John M. Pollard. “Factorization of the eighth Fermat number.” Mathematics of Computation 36(154) (1981), 627–630. DOI. Why read it: A concrete account of how a later Fermat number was factored, showing the computational tradition that grew from Euler’s division.
- J. C. Morehead, A. E. Western. “Note on Fermat’s numbers.” Bulletin of the American Mathematical Society 16(1) (1909), 1–6. DOI. Why read it: An early twentieth-century computation establishing that F7 is composite, useful for seeing how the Fermat-number problem moved from hand calculation to organised computation.
The mathematics behind this chapter: Chapter 10 — Every, Some, None (Part II, Chapter 1) (why five cases prove nothing and one refutes), Chapter 11 — What a Theorem Assumes (Part II, Chapter 2) (Fermat’s claim as the converse of a true theorem) and Chapter 13 — Exact Numbers and Misleading Aggregates (Part II, Chapter 4) (exact arithmetic).
Next: Chapter 3, where a theorem fails not because of one bad number, but because of a hypothesis nobody wrote down.