# 费马小定理

**费马小定理**是数论中的一个结果，它指出：若 \( p \) 为素数，\( a \) 为不被 \( p \) 整除的整数，则 \( a^{p-1} \equiv 1 \pmod{p} \)。一个等价形式也涵盖 \( p \mid a \) 的情形，即对每个整数 \( a \)，\( a^{p} \equiv a \pmod{p} \)。皮埃尔·德·费马（Pierre de Fermat）在1640年的通信中陈述了这一结果，莱昂哈德·欧拉（Leonhard Euler）于1736年发表了第一个证明；欧拉后来用他的欧拉函数（totient function）将其推广到合数模。该定理是RSA密码体制和费马素性检验（Fermat primality test）的基础。

| 关键事实 | 详情 |
|---|---|
| 陈述 | 若 \( p \) 为素数且 \( p \nmid a \)，则 \( a^{p-1} \equiv 1 \pmod{p} \)；等价地，对所有整数 \( a \)，\( a^p \equiv a \pmod{p} \)<sup>[1](https://www.dpmms.cam.ac.uk/~wtg10/fermat.html)</sup> |
| 首次陈述 | 费马在1640年与弗雷尼克尔·德·贝西（Frénicle de Bessy）的通信中披露，该通信始于请求找出一个大完全数<sup>[2](https://www.sciencedirect.com/science/article/pii/0315086091903714)</sup> |
| 首次发表的证明 | 欧拉于1736年在《某些与素数有关的定理的证明》（*Theorematum Quorundam ad Numeros Primos Spectantium Demonstratio*）中发表；莱布尼茨（Leibniz）在1683年以前的一份未发表手稿中给出了几乎相同的证明<sup>[3](https://donaldlab.cs.duke.edu/Teaching/discretemath/Online/lectures/l20e-fermat.html)</sup> |
| 推广 | 欧拉定理：对每个 \( a \in \mathbb{Z}_n^* \)，\( a^{\varphi(n)} \equiv 1 \pmod{n} \)，其中 \( \varphi \) 为欧拉函数；对于素数 \( p \)，\( \varphi(p) = p-1 \)<sup>[4](https://cs.cmu.edu/~goyal/15356/lecture_notes.pdf)</sup> |
| 有限域形式 | 在含 \( q \) 个元素的有限域 \( F \) 中，对每个非零 \( a \) 有 \( a^{q-1} = 1 \)，且对每个 \( a \) 有 \( a^q = a \)<sup>[5](https://ntouikan.ext.unb.ca/MATH4043/groups-section-applications.html)</sup> |
| 逆命题不成立 | 合数中的卡迈克尔（Carmichael）数——最小的为 \( 561 = 3 \cdot 11 \cdot 17 \)——对所有与 \( n \) 互素的 \( a \) 都满足 \( a^{n-1} \equiv 1 \pmod{n} \)<sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup> |
| 主要应用 | RSA公钥加密与费马式素性检验<sup>[7](https://dlmf.nist.gov/27.16)</sup><sup> • </sup><sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup> |

## 陈述与等价形式

该定理有两种常见表述。乘法形式说：当 \( p \) 为素数且 \( x \) 为不被 \( p \) 整除的整数时，\( x^{p-1} \) 模 \( p \) 同余于1<sup>[1](https://www.dpmms.cam.ac.uk/~wtg10/fermat.html)</sup>。只要 \( p \nmid a \)，把恒等式 \( a^p \equiv a \pmod{p} \) 除以 \( a \)，就得到这一形式<sup>[8](https://www.cs.uri.edu/cryptography/number-theory/fermats-little-theorem/fermat.html)</sup>。无限制形式 \( a^p \equiv a \pmod{p} \) 对所有整数 \( a \) 成立，包括 \( p \) 的倍数；此时两边都为0<sup>[5](https://ntouikan.ext.unb.ca/MATH4043/groups-section-applications.html)</sup>。

该陈述是素性的一个必要但不充分条件。对所有与 \( n \) 互素的 \( a \) 都满足 \( a^{n-1} \equiv 1 \pmod{n} \) 的合数称为卡迈克尔数<sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup>。

## 证明

**用拉格朗日（Lagrange）定理证明。** 把 \( x \) 视为模 \( p \) 的非零剩余类乘法群中的一个元素，该群阶为 \( p-1 \)。元素 \( x \) 生成一个循环子群，其阶 \( r \) 是满足 \( x^r = 1 \pmod{p} \) 的最小正整数。由拉格朗日定理，\( r \) 整除 \( p-1 \)，因此 \( x^{p-1} = (x^r)^{(p-1)/r} \equiv 1 \pmod{p} \)<sup>[1](https://www.dpmms.cam.ac.uk/~wtg10/fermat.html)</sup>。

**用计数剩余类证明。** 整数 \( 1, 2, \ldots, p-1 \) 构成模 \( p \) 的非零剩余类的一个完全集。若 \( a \not\equiv 0 \pmod{p} \)，则倍数 \( a, 2a, \ldots, (p-1)a \) 在模 \( p \) 下互不相同且非零，因此它们按某种次序构成同一个集合。把两个集合的所有元素相乘，得到 \( (p-1)! \equiv (p-1)!\, a^{p-1} \pmod{p} \)；又因为 \( p \) 与 \( (p-1)! \) 互素，所以 \( a^{p-1} \equiv 1 \pmod{p} \)<sup>[9](https://www.southampton.ac.uk/~wright/1001/fermats-little-theorem.html)</sup>。

**用二项式定理证明。** 只需对 \( x \) 归纳证明修改后的陈述 \( x^p \equiv x \pmod{p} \) 对每个 \( x \) 成立。若 \( x^p \equiv x \)，则按二项式定理展开 \( (x+1)^p \)，得 \( x^p + \binom{p}{1}x^{p-1} + \cdots + \binom{p}{p-1}x + 1 \)。对 \( 1 \le r \le p-1 \)，每个二项式系数 \( \binom{p}{r} \) 都被 \( p \) 整除，因此 \( (x+1)^p \equiv x^p + 1 \equiv x + 1 \pmod{p} \)<sup>[1](https://www.dpmms.cam.ac.uk/~wtg10/fermat.html)</sup>。

**欧拉1736年的证明。** 欧拉发表的证明先说明：对任意不等于2的素数，\( 2^{p-1} \equiv 1 \pmod{p} \)；再说明：对任意不等于3的素数，\( 3^{p-1} \equiv 1 \pmod{p} \)；最后用数学归纳法得出结论：该公式对所有与 \( p \) 互素的 \( a \) 成立<sup>[10](https://scholarlycommons.pacific.edu/euler-works/54/)</sup>。

## 欧拉的推广

欧拉把定理推广到合数模。欧拉函数 \( \varphi(N) \) 计数集合 \( \{1, 2, \ldots, N-1\} \) 中与 \( N \) 互素的整数；对于素数 \( p \)，\( \varphi(p) = p-1 \)；对于两个不同素数的乘积 \( N = p_1 p_2 \)，\( \varphi(N) = (p_1-1)(p_2-1) \)<sup>[4](https://cs.cmu.edu/~goyal/15356/lecture_notes.pdf)</sup>。欧拉定理指出，对每个 \( a \in \mathbb{Z}_n^* \)，\( a^{\varphi(n)} \equiv 1 \pmod{n} \)<sup>[4](https://cs.cmu.edu/~goyal/15356/lecture_notes.pdf)</sup>。令 \( n = p \)，即可重新得到费马小定理，因为 \( \varphi(p) = p-1 \)<sup>[11](https://docenti.ing.unipi.it/g.dini/Teaching/ssi/materiale-didattico/Basic-cryptography-Asymmetric-encryption-number_theory.pdf)</sup>。

该定理也可推广到所有有限域。若 \( F \) 是含 \( q \) 个元素的有限域，则 \( F^* = F \setminus \{0\} \) 在乘法下是阶为 \( q-1 \) 的群，因此对每个非零 \( a \in F \) 有 \( a^{q-1} = 1 \)；两边乘以 \( a \) 得到对每个 \( a \in F \) 有 \( a^q = a \)<sup>[5](https://ntouikan.ext.unb.ca/MATH4043/groups-section-applications.html)</sup>。

## 密码学应用

**RSA。** RSA密码以其创造者里维斯特（Rivest）、沙米尔（Shamir）和阿德尔曼（Adleman）命名，是20世纪70年代末发明的一种公钥密码；在这种密码中，发送方与接收方无需事先共享秘密密钥<sup>[12](https://pi.math.cornell.edu/~mec/2003-2004/cryptography/RSA/RSA.html)</sup>。编码者选择两个大的秘密素数 \( p \) 和 \( q \)，公布它们的乘积 \( n = pq \)，并选取一个不整除 \( p-1 \) 或 \( q-1 \) 的公开素数 \( r \)，把消息片段 \( x \) 编码为 \( y = x^r \bmod n \)。解码时，计算 \( s \)，即 \( r \) 模 \( \varphi(n) \) 的逆元，使得 \( rs = 1 + t\varphi(n) \)；由欧拉-费马定理 \( x^{\varphi(n)} \equiv 1 \pmod{n} \)，因此 \( y^s = x^{rs} \equiv x \pmod{n} \)<sup>[7](https://dlmf.nist.gov/27.16)</sup>。安全性依赖于寻找大素数与分解大整数之间在计算机耗时上的悬殊：以2010年已知的技术，分解一个由两个400位素数相乘得到的800位模数，在单台计算机上可能需要数十亿年<sup>[7](https://dlmf.nist.gov/27.16)</sup>。

**指数约化。** 该定理还能加速计算模素数的大幂。为计算 \( 561^{996001} \bmod 997 \)，把指数写成 \( 996 \cdot 1000 + 1 \)；因为 \( 996 = 997 - 1 \)，费马小定理给出 \( 561^{996 \cdot 1000} \equiv 1 \pmod{997} \)，所以整个幂约化为 \( 561 \pmod{997} \)<sup>[8](https://www.cs.uri.edu/cryptography/number-theory/fermats-little-theorem/fermat.html)</sup>。

## 素性检验

该定理启发了费马检验：给定 \( n \)，从 \( \{1, \ldots, n-1\} \) 中均匀随机选取 \( x \)，若 \( x^{n-1} \equiv 1 \pmod{n} \) 则输出“素数”，否则输出“合数”<sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup>。若 \( n \) 为素数，检验总是回答“素数”。其弱点在于卡迈克尔数，即像 \( 561 = 3 \cdot 11 \cdot 17 \) 这样的合数 \( n \)，它们对每个与 \( n \) 互素的底数都能通过检验；这样的数有无穷多个，并且在任意给定的 \( n \) 以下至少有 \( n^{2/7} \) 个；这一事实在埃尔德什（Erdős）提出猜想之后于1994年被严格证明<sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup>。对任何不是卡迈克尔数的合数 \( n \)，费马检验输出“合数”的概率至少为 \( 1/2 \)；重复 \( d \) 次后，这一概率提高到 \( 1 - 2^{-d} \)<sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup>。

米勒-拉宾检验通过反复取 \( a^{n-1} \) 的平方根来强化这一思想：将 \( n - 1 \) 写成 \( 2^s q \)，其中 \( q \) 为奇数，它检查序列 \( a^q, a^{2q}, \ldots, a^{2^s q} \)；对素数 \( n \)，该序列必须以1结尾，且其中最后一个非1元素（若存在）在模 \( n \) 下为 \(-1\)<sup>[16](https://zoo.cs.yale.edu/classes/cs467/2006f/attach/ln13.html)</sup>。对任何合数 \( n \)（包括卡迈克尔数），通过检验的概率至多为 \( 1/4 \)<sup>[13](https://crypto.stanford.edu/pbc/notes/numbertheory/millerrabin.html)</sup>。

一个多项式版本恢复了精确刻画：\( n \) 为素数当且仅当对所有 \( a \in \mathbb{Z}_n^* \)，\( (x - a)^n \equiv x^n - a \pmod{n} \)<sup>[6](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)</sup>。这一恒等式是阿格拉沃尔（Agrawal）、卡亚尔（Kayal）和萨克塞纳（Saxena）于2002年发表的AKS素性检验的基础；该检验是第一个在输入长度上以多项式时间运行的确定性素性检验<sup>[14](https://math.mit.edu/~drew/PrimesInP.pdf)</sup>。

## 相关结果

威尔逊（Wilson）定理最早由拉格朗日于1770年证明，它给出一个双向的素性判别法：整数 \( n > 1 \) 为素数当且仅当 \( (n-1)! \equiv -1 \pmod{n} \)<sup>[9](https://www.southampton.ac.uk/~wright/1001/fermats-little-theorem.html)</sup>。费马小定理不同于费马大定理；后者断言：当 \( n > 2 \) 时，方程 \( a^n + b^n = c^n \) 在非零自然数中没有解，由安德鲁·怀尔斯（Andrew Wiles）于1994年证明<sup>[15](https://www.lemonde.fr/sciences/article/2025/02/18/le-dernier-theoreme-de-fermat-un-monument-des-maths-auquel-s-attaque-une-collaboration-internationale_6551843_1650684.html)</sup>。

## References

1. [W. T. Gowers, Fermat's Little Theorem, DPMMS, University of Cambridge](https://www.dpmms.cam.ac.uk/~wtg10/fermat.html)
2. [Colin R. Fletcher, A reconstruction of the Frenicle-Fermat correspondence of 1640, Historia Mathematica 18 (1991)](https://www.sciencedirect.com/science/article/pii/0315086091903714)
3. [Fermat's Little Theorem, lecture notes, Duke University](https://donaldlab.cs.duke.edu/Teaching/discretemath/Online/lectures/l20e-fermat.html)
4. [Vipul Goyal, Lecture Notes on Introduction to Cryptography, Carnegie Mellon University](https://cs.cmu.edu/~goyal/15356/lecture_notes.pdf)
5. [AATA Section 7.3: Applications to Field Theory](https://ntouikan.ext.unb.ca/MATH4043/groups-section-applications.html)
6. [CS265/CME309: Randomized Algorithms, Lecture 3, Stanford University](https://web.stanford.edu/class/archive/cs/cs265/cs265.1224/Lectures/Lecture3/l3.pdf)
7. [DLMF §27.16 Cryptography, NIST](https://dlmf.nist.gov/27.16)
8. [Fermat's Little Theorem, University of Rhode Island cryptography notes](https://www.cs.uri.edu/cryptography/number-theory/fermats-little-theorem/fermat.html)
9. [MATH1001 §6.2 Fermat's Little Theorem, University of Southampton](https://www.southampton.ac.uk/~wright/1001/fermats-little-theorem.html)
10. [Leonhard Euler, Theorematum Quorundam ad Numeros Primos Spectantium Demonstratio (1736), Euler Archive](https://scholarlycommons.pacific.edu/euler-works/54/)
11. [Modern Cryptography: Theory and Practice, course notes, University of Pisa](https://docenti.ing.unipi.it/g.dini/Teaching/ssi/materiale-didattico/Basic-cryptography-Asymmetric-encryption-number_theory.pdf)
12. [Primes, Modular Arithmetic, and Public Key Cryptography II, Cornell University](https://pi.math.cornell.edu/~mec/2003-2004/cryptography/RSA/RSA.html)
13. [Number Theory: Primality Tests, Stanford crypto notes](https://crypto.stanford.edu/pbc/notes/numbertheory/millerrabin.html)
14. [18.095 Lecture 8.4: A deterministic primality test, MIT](https://math.mit.edu/~drew/PrimesInP.pdf)
15. [Le dernier théorème de Fermat, Le Monde (18 February 2025)](https://www.lemonde.fr/sciences/article/2025/02/18/le-dernier-theoreme-de-fermat-un-monument-des-maths-auquel-s-attaque-une-collaboration-internationale_6551843_1650684.html)
16. [zoo.cs.yale.edu](https://zoo.cs.yale.edu/classes/cs467/2006f/attach/ln13.html)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Elementary number theory › Orders, primitive roots, and the multiplicative group mod n

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/fermats-little-theorem

English original: https://www.edgepedia.org/fermats-little-theorem
