Example

Refute Fermat's prime formula with one division

Compute the first six numbers 2^(2^n) + 1, find Euler's 641, and see why one counterexample settles a universal claim.

Fermat believed every number of the form 2^(2^n) + 1 is prime. The first five are, and he stopped there. The chapter’s claim is that a universal statement needs only one counterexample, and that five confirming cases add nothing beyond themselves. This program finds the counterexample Euler reported in 1732: the sixth number is divisible by 641.

Run it

Save this as fermat.py and run it with Python 3.11 or later. It uses the standard library only.

"""Fermat's numbers and Euler's 641, in exact integer arithmetic (standard library only)."""


def fermat(n):
    return 2 ** (2 ** n) + 1


def smallest_factor(m):
    """The smallest divisor of m greater than 1, by trial division."""
    d = 2
    while d * d <= m:
        if m % d == 0:
            return d
        d += 1
    return m


print("n  2^(2^n) + 1   smallest factor > 1")
for n in range(6):
    f = fermat(n)
    p = smallest_factor(f)
    print(f"{n}  {f:<12} {p:<10} {'prime' if p == f else 'composite'}")

f5 = fermat(5)
print()
print(f"2^32 + 1            = {f5}")
print(f"remainder mod 641   = {f5 % 641}")
print(f"cofactor            = {f5 // 641}")
print(f"641 * 6700417 == F5 : {641 * 6700417 == f5}")
print(f"6700417 is prime    : {smallest_factor(6700417) == 6700417}")
print()
# Failure control: a wrong divisor must not pass.
print(f"control: remainder mod 643 = {f5 % 643} (643 does not divide F5)")

Expected outcome

n  2^(2^n) + 1   smallest factor > 1
0  3            3          prime
1  5            5          prime
2  17           17         prime
3  257          257        prime
4  65537        65537      prime
5  4294967297   641        composite

2^32 + 1            = 4294967297
remainder mod 641   = 0
cofactor            = 6700417
641 * 6700417 == F5 : True
6700417 is prime    : True

control: remainder mod 643 = 572 (643 does not divide F5)

Five primes, then a composite. The remainder 0 is the whole argument: since 1 < 641 < 4294967297, the sixth number has a divisor other than 1 and itself, so the claim “every such number is prime” is false. The control matters as much as the result: a check that would also accept 643 would prove nothing.

What the laboratory checked

The recorded run 20261113T161233Z-9c5ae090 (evidence ) is the book’s result; the program above is a reader-sized demonstration of the same mathematics. Evidence: Fresh kernel check (Lean with Mathlib, and Rocq) plus exact computation. Status: kernel_checked_correspondence_approved.

For every natural number n, the number 2^(2^n) + 1 is prime.

Check System Recorded outcome
lean Lean kernel 7 of 7 declarations accepted and bound to their pinned statements
rocq Rocq kernel 4 of 4 declarations accepted and bound to their pinned statements
compute Python + SymPy (exact integer arithmetic) exit 0, verdict refuted

Failure controls: 9 of 9 rejected or refused as required (lean-wrong-divisor, lean-wrong-cofactor, lean-wrong-index, rocq-wrong-divisor, rocq-wrong-cofactor, rocq-wrong-index, lean-binding-wrong-statement, rocq-binding-wrong-statement, compute-wrong-divisor). Correspondence N1-CORR-01 (does the formal statement say what the claim means?): approved by the author. Contract N1-FERMAT-01 version 1, sha256 ef384bbfd65f8bda…; result.json sha256 f76130c9a7e64a23….

To reproduce the laboratory’s run, build the math-env image from a clone of the repository (Getting Started ) and run:

img=$(docker image inspect math-env --format '{{.Id}}')
docker run --rm --network none -e UV_OFFLINE=1 -e MATHENV_IMAGE_ID=$img -v "${PWD}:/source:ro" -v "${PWD}/out:/out" math-env uv run --offline math-engine proof fermat --out /out/proof-fermat

This Docker form was tested for fermat (R-D06). To re-verify the recorded run itself without Docker, math-engine proof --verify experiments/investigations/n1-fermat/kernel-runs/2026-11-13-approved (R-V01).

Mechanism and limitations

Documented: the claim contract experiments/investigations/n1-fermat/investigation.json (version 1) pins the statements, checks and controls before anything runs; the recorded run directory holds result.json, the runner’s notebook and a SHA256SUMS manifest of every file.

Observed: the program’s output above, and the recorded run 20261113T161233Z-9c5ae090: Kernel-checked on the pinned toolchain, with the correspondence approved by the author. Human mathematical review is recorded separately.

Not shown, as the laboratory’s own record states:

  • Anything about 2^(2^n) + 1 for n > 5. The numbers for n = 5 to 32 are known to be composite (external literature, not checked here). Whether the number for n = 33 is prime is unknown. Whether there are further Fermat primes beyond 65537 is open.
  • How Euler found 641. E26 does not say, and later explanations are historians’ reconstructions.
  • What exactly Fermat wrote, until the primary transcription is done.
  • That the Lean and Rocq statements say the same thing. That is the correspondence (N1-CORR-01), which awaits the author’s approval. (Since approved by the author: see correspondence N1-CORR-01 above.)

Challenge this result

Copy this into the AI assistant of your choice, or work through it yourself. Check any finding against the recorded evidence.

Examine the claim that 2^32 + 1 = 641 × 6700417. Verify the arithmetic
independently, then inspect the formal statements and the nine failure
controls: do the Lean and Rocq statements both say that Fermat's universal
claim is false, and would any control have passed if the check were broken?
Distinguish an error in a proof from an error in the correspondence between
the formal theorem and the intended claim, and propose a stronger investigation.

Chapter: https://programmer.ie/books/math/02-chapter/
Example: https://programmer.ie/examples/math/02-chapter/
Recorded run: N1-FERMAT-01 20261113T161233Z-9c5ae090, status kernel_checked_correspondence_approved; evidence: Fresh kernel check (Lean with Mathlib, and Rocq) plus exact computation
Source and evidence: https://github.com/ernanhughes/math/tree/main/experiments/investigations/n1-fermat/kernel-runs/2026-11-13-approved
Distinguish what the record shows from what you infer. If you cannot open a link, ask me for the material; do not invent it.

Canonical source , the recorded run and the program .

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.