# 费马两平方和定理

**费马两平方和定理**指出，一个奇素数能写成两个完全平方数之和，当且仅当它模 4 同余于 1；素数 2 = 1² + 1² 也属于此类，而任何模 4 同余于 3 的素数都不能这样表示。<sup>[1](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)</sup><sup> • </sup><sup>[2](https://leanprover.github.io/logic_and_proof_lean3/elementary_number_theory.html)</sup> 例如，5 = 1² + 2²，97 = 4² + 9²，二者都是形如 4n + 1 的素数；而 3、7、11、19 都不是两平方和。<sup>[1](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)</sup> 对于形如 4k + 1 的素数，除两个加数的次序外，表示方式是唯一的。<sup>[3](https://proofwiki.org/wiki/Fermat%27s_Two_Squares_Theorem)</sup>

| 关键事实 | 细节 |
|---|---|
| 陈述 | 奇素数 p 为两平方和当且仅当 p ≡ 1 (mod 4)；p = 2 = 1² + 1² 也符合条件<sup>[1](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)</sup> |
| 唯一性 | 对于素数 4k + 1，表达式 p = x² + y² 在不计 x 与 y 的次序时唯一<sup>[3](https://proofwiki.org/wiki/Fermat%27s_Two_Squares_Theorem)</sup> |
| 首次陈述 | 阿尔伯特·吉拉尔（Albert Girard）于 1632 年未加证明地提出；费马在 1654 年致帕斯卡的信中声称有证明，但无记录留存<sup>[4](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)</sup> |
| 首个证明 | 欧拉（Euler）的完整证明首见于 1749 年 4 月 12 日致哥德巴赫（Goldbach）的信，使用了下降法<sup>[5](https://arxiv.org/abs/1404.0187)</sup> |
| 一般整数 | 正整数为两平方和当且仅当其每个模 4 同余于 3 的素因子都以偶次幂出现<sup>[5](https://arxiv.org/abs/1404.0187)</sup> |
| 著名现代证明 | 唐·扎吉尔（Don Zagier）的一句话证明，《美国数学月刊》97(2)（1990 年），第 144 页，浓缩了希思-布朗（Heath-Brown）1984 年的证明<sup>[6](https://arxiv.org/abs/2112.02556)</sup> |

## 为何模 4 余 3 的素数不成立

障碍在模 4 下清晰可见。偶数的平方 (2k)² = 4k² 模 4 同余于 0，奇数的平方 (2k + 1)² = 4(k² + k) + 1 模 4 同余于 1，因此两个平方数之和模 4 只能同余于 0、1 或 2，绝不可能是 3。<sup>[4](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)</sup> 这一下就排除了所有模 4 同余于 3 的数 n，无论是否为素数。<sup>[4](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)</sup>

更精细的论证用到费马小定理。若对素数 p ≡ 3 (mod 4)，同余式 x² ≡ −1 (mod p) 有解，则两边取 (p − 1)/2 次幂将给出 x^(p−1) ≡ (−1)^((p−1)/2) ≡ −1 (mod p)，这与费马小定理矛盾，因为后者强制 x^(p−1) ≡ 1 (mod p)。<sup>[1](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)</sup> 所以 −1 是模 p 的平方元恰好当 p = 2 或 p ≡ 1 (mod 4)，而这一条件等价于 p 为两平方和。<sup>[1](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)</sup>

## 逆命题及其证明

困难的方向是存在性：每个素数 p = 4m + 1 实际上都能写成某对自然数 x 与 y 的平方和 x² + y²。<sup>[7](https://firsching.ch/FormalBook/blueprint/sect0004.html)</sup> 一个初等证明（图厄格点论证）考虑集合 {0, 1, …, ⌊√p⌋}² 中的 (⌊√p⌋ + 1)² 对整数，其数目多于 p，因此对于满足 s² ≡ −1 (mod p) 的固定 s，必有两对不同的整数在模 p 下关于 x′ − sy′ 同余。取差便得到 x、y，满足 0 < x² + y² < 2p 且 x² + y² ≡ 0 (mod p)，从而迫使 x² + y² = p。<sup>[7](https://firsching.ch/FormalBook/blueprint/sect0004.html)</sup>

**扎吉尔的对合证明。** 最著名的简短证明考虑有限集 S = {(x, y, z) ∈ ℤ³ : x² + 4yz = p, x > 0, y > 0, z > 0}，并在其上定义两个对合。<sup>[19](https://www.math.gordon.edu/ntic/ntic2021/section-one-sentence.html)</sup> 其中一个对合是显然的；另一个恰有一个不动点，因此该集合的基数为奇数，这迫使第一个对合也有不动点，而该不动点给出 p = x² + y²。<sup>[6](https://arxiv.org/abs/2112.02556)</sup><sup> • </sup><sup>[7](https://firsching.ch/FormalBook/blueprint/sect0004.html)</sup> 扎吉尔 1990 年的一句话版本浓缩了罗杰·希思-布朗 1984 年的证明，后者又借鉴了与约瑟夫·刘维尔（Joseph Liouville）相关的工作；该证明按原文所述是非构造性的，但后来在 HOL4 定理证明器中形式化了一个可构造的变体，能给出求两平方的算法。<sup>[6](https://arxiv.org/abs/2112.02556)</sup>

**费马本人的方法。** 欧拉的完整证明首见于 1749 年 4 月 12 日致哥德巴赫的信，使用了下降法——这是费马为证明某些丢番图方程无解而引入的技巧：从任一表示可推出一个更小的表示，而这在正整数中是不可能的。<sup>[5](https://arxiv.org/abs/1404.0187)</sup> 关于欧拉书面证明的发表时间，各家来源说法不一：LibreTexts 数论教材称欧拉于 1758 年发表了一个证明，<sup>[4](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)</sup> 而一篇关于该定理历史的综述则称他于 1749 年宣布、1760 年发表。<sup>[8](https://cpoli.github.io/mathematicskit/history/number_theory_breakthroughs.html)</sup>

## 高斯整数视角

现代的结构性解释在高斯整数环 ℤ[i] = {a + bi : a, b ∈ ℤ} 中进行，该环由卡尔·弗里德里希·高斯（Carl Friedrich Gauss）于 1832 年研究双二次互反律时引入。<sup>[9](https://stephenratliff.github.io/learn/gaussian-integers.html)</sup> 范数 N(a + bi) = a² + b² 是乘性的，因此问素数 p 是否为两平方和，就是问 p 在 ℤ[i] 中能否分解为 (a + bi)(a − bi)。<sup>[10](https://www.matpic.com/essays/two-squares-and-a-lattice/)</sup> 由于 ℤ[i] 具有唯一分解性，有理素数要么在其中保持素性，要么分裂为两个共轭因子，而究竟发生哪种情况，取决于 −1 是否为模 p 的平方元。<sup>[10](https://www.matpic.com/essays/two-squares-and-a-lattice/)</sup> 素数 p ≡ 3 (mod 4) 在 ℤ[i] 中仍为素数；素数 p ≡ 1 (mod 4) 则分裂，每次分裂给出表示 p = N(a + bi) = a² + b²。<sup>[11](https://crypto.stanford.edu/pbc/notes/numberfield/sumsquares.html)</sup>

范数的乘性解释了为何两平方和之积仍是两平方和，这通过恒等式 (a² + b²)(c² + d²) = (ad − bc)² + (ac + bd)² 实现。<sup>[11](https://crypto.stanford.edu/pbc/notes/numberfield/sumsquares.html)</sup> 它也解释了合数如何获得多种表示：485 = 5 · 97 可写成 14² + 17² 或 22² + 1²，对应于从每对共轭因子中各选一个因子的两种方式。<sup>[1](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)</sup> 类似地，1010 = 2 · 5 · 101 = (1+i)(1−i)(2+i)(2−i)(10+i)(10−i) 恰有两种本质不同的表示，其中包括 7² + 31²，这里把仅次序或符号不同的表示视为同一种。<sup>[12](https://web.math.utk.edu/~dcartwr1/iaawa/domains-5.html)</sup>

## 所有整数，而不只是素数

素数情形可推广为完整的刻画：正整数 z 为两平方和当且仅当 z 的素因子分解中每个模 4 同余于 3 的素因子 q 都以偶次幂出现。<sup>[5](https://arxiv.org/abs/1404.0187)</sup> 等价地，正整数为两平方和恰好当它具有形式 a²b，其中 b 无平方因子且不含模 4 同余于 3 的素因子。<sup>[11](https://crypto.stanford.edu/pbc/notes/numberfield/sumsquares.html)</sup> 任何素数的偶次幂都平凡地是两平方和，因为 p^(2n) = (p^n)² + 0²。<sup>[4](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)</sup> 正整数能表示为两平方和当且仅当其素因子分解中每个模 4 同余于 3 的素数都以偶次幂出现，因此形如 2^m(4n + 1) 的数未必是这种和。<sup>[13](https://www.maths.tcd.ie/pub/HistMath/People/Fermat/RouseBall/RB_Fermat.html)</sup><sup> • </sup><sup>[20](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_(Barrus_and_Clark)/01:_Chapters/1.28:_Sum_of_Squares)</sup>

该定理还与 −1 的平方根条件相联系，其形式现已在 Lean 库 Mathlib 中形式化：自然数 n 为两平方和当且仅当 n = a² · b 且 −1 是模 b 的平方元；并且若 n 是互素整数的两平方和，则 −1 是模 n 的平方元。<sup>[14](https://leanprover-community.github.io/mathlib4_docs/Mathlib/NumberTheory/SumTwoSquares.html)</sup>

## 历史与形式化

该定理的陈述有着层层叠叠的历史。阿尔伯特·吉拉尔于 1632 年未加证明地陈述了它；费马在 1654 年致帕斯卡的信中声称有证明，但无记录留存；欧拉发表了首个证明。<sup>[4](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)</sup> MacTutor 的素数史记载，费马证明了吉拉尔的猜想：每个形如 4n + 1 的素数都能以唯一方式写成两平方和。<sup>[15](https://mathshistory.st-andrews.ac.uk/HistTopics/Prime_numbers/)</sup>

该定理已成为交互式定理证明中的标准基准。Mathlib 包含完整两平方和刻画的证明，由 2024 年 3 月在吕米尼举行的“Lean For the Curious Mathematician”会议团队形式化。<sup>[14](https://leanprover-community.github.io/mathlib4_docs/Mathlib/NumberTheory/SumTwoSquares.html)</sup><sup> • </sup><sup>[16](https://imperialcollegelondon.github.io/FLT/blueprint.pdf)</sup> 数学组件库中存在一个使用高斯整数的 Coq 证明，扎吉尔一句话论证的形式化证明也已在 Coq 中给出。<sup>[17](https://github.com/thery/twoSquare)</sup><sup> • </sup><sup>[18](https://arxiv.org/abs/2103.11389)</sup>

## References

1. [Number Theory III: Notes (Jack Shotton, Durham University), Theorems 3.1–3.6](https://www.maths.dur.ac.uk/users/jack.g.shotton/Teaching/number-theory/notes.html)
2. [Logic and Proof, Elementary Number Theory (Lean Prover community)](https://leanprover.github.io/logic_and_proof_lean3/elementary_number_theory.html)
3. [Fermat's Two Squares Theorem, ProofWiki](https://proofwiki.org/wiki/Fermat%27s_Two_Squares_Theorem)
4. [1.28: Sum of Squares, Elementary Number Theory (Barrus and Clark), Mathematics LibreTexts](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.28%3A_Sum_of_Squares)
5. [Representing Integers as the Sum of Two Squares in the Ring ℤ_n (arXiv:1404.0187)](https://arxiv.org/abs/1404.0187)
6. [Windmills of the Minds: An Algorithm for Fermat's Two Squares Theorem (arXiv:2112.02556)](https://arxiv.org/abs/2112.02556)
7. [Representing numbers as sums of two squares, FormalBook blueprint (Firsching)](https://firsching.ch/FormalBook/blueprint/sect0004.html)
8. [Breakthroughs in Number Theory, mathematicskit documentation](https://cpoli.github.io/mathematicskit/history/number_theory_breakthroughs.html)
9. [Gaussian Integers — A Complete, Interactive Lesson](https://stephenratliff.github.io/learn/gaussian-integers.html)
10. [Two squares, and a lattice, Matpic](https://www.matpic.com/essays/two-squares-and-a-lattice/)
11. [Introduction — Sum of Two Squares, Stanford cryptography notes](https://crypto.stanford.edu/pbc/notes/numberfield/sumsquares.html)
12. [IAAWA: Factorization of Gauss integers, University of Tennessee](https://web.math.utk.edu/~dcartwr1/iaawa/domains-5.html)
13. [Pierre de Fermat (1601–1665), Rouse Ball, Trinity College Dublin](https://www.maths.tcd.ie/pub/HistMath/People/Fermat/RouseBall/RB_Fermat.html)
14. [Mathlib.NumberTheory.SumTwoSquares documentation](https://leanprover-community.github.io/mathlib4_docs/Mathlib/NumberTheory/SumTwoSquares.html)
15. [Prime numbers, MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk/HistTopics/Prime_numbers/)
16. [Fermat's Last Theorem blueprint, Imperial College London](https://imperialcollegelondon.github.io/FLT/blueprint.pdf)
17. [thery/twoSquare: A proof of Fermat's theorem on sum of two squares with mathcomp using Gaussian integers](https://github.com/thery/twoSquare)
18. [Formal verification of Zagier's one-sentence proof (arXiv:2103.11389)](https://arxiv.org/abs/2103.11389)
19. [NTIC A One-Sentence Proof](https://www.math.gordon.edu/ntic/ntic2021/section-one-sentence.html)
20. [1.28: Sum of Squares](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_(Barrus_and_Clark)/01:_Chapters/1.28:_Sum_of_Squares)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Elementary number theory › Quadratic residues and reciprocity

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/fermats-theorem-on-sums-of-two-squares

English original: https://www.edgepedia.org/fermats-theorem-on-sums-of-two-squares
