# 模

**模**（Modulo，拉丁语意为“按照模的方式”）既指余数运算，写作 `a mod m` 或 `a % b`，即一个整数除以另一个整数后所得的余数；也指同余关系 \( a \equiv b \pmod m \) 中的介词，表示 \( a \) 与 \( b \) 在除以 \( m \) 时余数相同。同余关系及相关算术由卡尔·弗里德里希·高斯（Carl Friedrich Gauss）在1801年出版的《算术研究》（*Disquisitiones Arithmeticae*）中引入，如今模算术构成了哈希、伪随机数生成、计算机算术和公钥密码学的基础。<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup><sup> • </sup><sup>[2](https://la.wikisource.org/wiki/Disquisitiones_arithmeticae/Sectio_prima)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 当 \( m \mid (a - b) \) 时，\( a \equiv b \pmod m \)；除数 \( m \) 称为模<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> |
| 起源 | 卡尔·弗里德里希·高斯，《算术研究》，1801年，出版时年方21岁<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> |
| 带余除法 | 对任意整数 \( n \) 和正整数 \( d \)，存在唯一的整数 \( q \) 和 \( r \)，满足 \( n = qd + r \) 且 \( 0 \le r < d \)<sup>[3](https://math.umd.edu/~pbrosnan/notes/ugalg/sect0005.html)</sup> |
| 代数性质 | 同余具有自反性、对称性和传递性；加法、减法和乘法均保持同余，但消去与模不互质的因子时消去律不成立<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> |
| 编程形式 | `%` 运算符：在Java中接受整数和浮点操作数，符号随被除数；在C中仅接受整数类型，除零为未定义行为<sup>[4](https://docs.oracle.com/javase/specs/jls/se15/html/jls-15.html)</sup><sup> • </sup><sup>[5](https://cppreference.net/c/language/operator_arithmetic.html)</sup> |
| 密码学用途 | RSA将消息编码为 \( y \equiv x^{r} \pmod n \)，其中 \( n = pq \) 公开；解码需要秘密因子 \( p \) 和 \( q \)<sup>[6](https://dlmf.nist.gov/27.16)</sup> |
| 日常应用 | 哈希、伪随机数生成、移位密码，以及处理器中的补码算术<sup>[7](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture11.html)</sup> |

## 同余与模

高斯的定义见于《算术研究》第一节“论数的同余”（De numerorum congruentia in genere）：若一个数 \( a \) 整除数 \( b \) 与 \( c \) 之差，则称 \( b \) 与 \( c \) 关于 \( a \) 同余，\( a \) 称为模；\( b \) 与 \( c \) 互为对方的剩余，反之则称为非剩余。<sup>[2](https://la.wikisource.org/wiki/Disquisitiones_arithmeticae/Sectio_prima)</sup> 用现代记号表述：对固定的整数 \( m \)，整数 \( a \) 与 \( b \) 模 \( m \) 同余，记作 \( a \equiv b \pmod m \)，当且仅当 \( m \) 整除 \( a - b \)；否则记作 \( a \not\equiv b \pmod m \)。<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> 举例来说，17与5作为数虽然不同，但二者除以3的余数都是2，因此它们模3同余。<sup>[8](https://a-warded.org/open-book/university-discrete-mathematics/lesson-18)</sup>

余数本身由带余除法确定：对任意整数 \( n \) 和正整数 \( d \)，存在唯一的整数 \( q \)（商）和 \( r \)（余数），使得 \( n = qd + r \) 且 \( 0 \le r < d \)。<sup>[3](https://math.umd.edu/~pbrosnan/notes/ugalg/sect0005.html)</sup> 余数可用下取整函数表示为 \( r = n - d \lfloor n/d \rfloor \)。<sup>[3](https://math.umd.edu/~pbrosnan/notes/ugalg/sect0005.html)</sup> 时钟是最标准的例证：小时按模12运行，因此8点之后7小时是3点。

## 代数性质

同余对基本运算的行为类似于等号。它具有自反性（\( a \equiv a \)）、对称性和传递性，这正是等价关系的三个性质。<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> 若 \( a \equiv a' \) 且 \( b \equiv b' \pmod m \)，则 \( a + b \equiv a' + b' \)、\( a - b \equiv a' - b' \)、\( a \cdot b \equiv a' \cdot b' \pmod m \)。<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> 自19世纪末以来，高斯引入同余关系一直被描述为现代数学中第一个等价关系的定义。<sup>[9](https://ghostarchive.org/pdfarc/WcLBP)</sup>

除法是例外。尽管 28 ≡ 10 (mod 6) 且 2 同时整除 28 和 10，但将同余式两边除以2却不成立：14 与 5 模6并不同余。只有当被消去的因子与模互质时，消去律才成立，这一限制可由最大公约数推出。<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup>

## 历史

高斯发展了模算术，并于1801年在他21岁时在《算术研究》中发表。<sup>[1](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)</sup> 该书历经长期拖延方才问世，其七个部分每一部分都引起了国际关注；各章涵盖算术与同余理论、欧拉关于二次互反律的猜想的证明、二次型、连分数、素性检验以及分圆方程。<sup>[10](https://mathshistory.st-andrews.ac.uk/Strick/gauss.pdf)</sup> 高斯在序言中提到了他的先驱费马（Pierre de Fermat）、欧拉（Leonhard Euler）、拉格朗日（Joseph-Louis Lagrange），尤其是勒让德（Adrien-Marie Legendre），后者1797年的《数论试论》（*Essai sur la théorie des nombres*）与《算术研究》在时间上有所重叠。<sup>[10](https://mathshistory.st-andrews.ac.uk/Strick/gauss.pdf)</sup> “模”记号 \( a \equiv b \bmod n \)，读作“a与b模n同余”，出现在高斯后来关于同余的计算中，包括他1816年的复活节算法。<sup>[10](https://mathshistory.st-andrews.ac.uk/Strick/gauss.pdf)</sup> 一份同时代的记述指出，他的数论研究始于哥廷根（Göttingen）求学期间，而关于数的同余的处理见于1801年的著作。<sup>[11](https://en.wikisource.org/wiki/Page%3AA_Source_Book_in_Mathematics_%28Smith%29.pdf/131)</sup>

## 编程中的模

编程语言将余数运算暴露为二元 `%` 运算符，而不同语言对负操作数的处理各不相同，因为底层的除法可能向不同方向取整。

**Java。** Java语言规范将 `%` 定义为隐式除法的余数，满足恒等式 `(a/b)*b+(a%b)` 等于 `a`。结果仅当被除数为负时才为负，仅当被除数为正时才为正，其绝对值始终小于除数的绝对值：`5%3` 为2，`5%(-3)` 为2，`(-5)%3` 为-2，`(-5)%(-3)` 为-2。若整数除数为0，则抛出 `ArithmeticException`。与C和C++不同，Java还接受浮点操作数，将 `%` 定义为向零取整的除法所得的余数，类似于C库函数 `fmod`。<sup>[4](https://docs.oracle.com/javase/specs/jls/se15/html/jls-15.html)</sup>

**C与C++。** 二元 `%` 运算符返回第一个操作数除以第二个操作数的余数，且余数运算符不适用于浮点类型；库函数 `fmod` 提供该功能。自C99起，整数除法向零截断，因此余数的符号随被除数；在此之前，符号由实现定义。若第二个操作数为零，行为未定义，且在补码系统上 `INT_MIN % -1` 为未定义行为。<sup>[5](https://cppreference.net/c/language/operator_arithmetic.html)</sup> 在Python中，operator模块将语法 `a % b` 映射为函数 `mod(a, b)`。<sup>[12](https://github.com/python/cpython/blob/main/Doc/library/operator.rst)</sup>

## 应用

**哈希。** 当需要将取自大范围 \( \{0, 1, \ldots, M-1\} \) 的少量值放入紧凑的槽位集合 \( \{0, 1, \ldots, n-1\} \) 时，哈希函数计算 \( \mathrm{hash}(x) = x \bmod p \)，或者 \( (ax + b) \bmod p \)，其中 \( p \) 是接近 \( n \) 的素数。利用数据的全部比特有助于避免相似值之间的冲突。<sup>[7](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture11.html)</sup>

**伪随机数生成。** 线性同余法由 \( x_{n+1} = (a x_n + c) \bmod m \) 生成序列，其中 \( x_0 \)、\( a \)、\( c \) 和 \( m \) 为选定参数。<sup>[7](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture11.html)</sup>

**密码。** 凯撒密码或称移位密码将字母视为数字 A = 0、B = 1，依此类推，用 \( f(p) = (p + k) \bmod 26 \) 编码，用 \( f^{-1}(p) = (p - k) \bmod 26 \) 解码；更一般的仿射版本使用 \( (ap + b) \bmod 26 \)。<sup>[7](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture11.html)</sup>

**公钥密码学。** RSA式公钥密码利用了寻找大素数与分解大整数在计算机时间上的巨大差距。编码者选取两个各约400位十进制数字的大秘密素数 \( p \) 和 \( q \)，并公布其乘积 \( n = pq \)，一个800位的数。消息片段 \( x \) 用公开素数 \( r \) 编码为 \( y \equiv x^{r} \pmod n \)；解码时，接收方计算 \( r \) 模欧拉函数（即不超过n且与n互质的整数个数）\( \varphi(n) \) 的逆元 \( s \)，使得 \( y^{s} \equiv x \pmod n \)。以2010年已知的最有效技术，在单台计算机上分解一个800位的数可能需要数十亿年，因此在目前关于分解的知识水平下，这类密码被认为是不可破译的。<sup>[6](https://dlmf.nist.gov/27.16)</sup> 大学密码学课程因此将数论基础建立在模算术、欧几里得算法、欧拉定理、中国剩余定理和模幂运算之上，然后才转向迪菲-赫尔曼密钥交换、ElGamal加密和椭圆曲线系统。<sup>[13](https://www.cl.cam.ac.uk/teaching/2526/Crypto/)</sup>

**计算机算术。** 模算术被描述为计算的基础：它与补码表示配合用于实现处理器算术，其中 \( n \) 位寄存器将 \( -|x| \) 表示为无符号值 \( 2^{n} - |x| \)。<sup>[7](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture11.html)</sup>

## References

1. [Modular Arithmetic, PGSS Computational Discrete Mathematics Notes, Carnegie Mellon University](https://www.math.cmu.edu/~rw1k/pgss/CompDiscMathNotes.pdf)
2. [Disquisitiones arithmeticae, Sectio prima, Wikisource (Latin)](https://la.wikisource.org/wiki/Disquisitiones_arithmeticae/Sectio_prima)
3. [The division algorithm with remainder, UMD 403: Undergraduate Algebra](https://math.umd.edu/~pbrosnan/notes/ugalg/sect0005.html)
4. [Chapter 15. Expressions, The Java Language Specification, SE 15, Oracle](https://docs.oracle.com/javase/specs/jls/se15/html/jls-15.html)
5. [Arithmetic operators, C reference, cppreference.net](https://cppreference.net/c/language/operator_arithmetic.html)
6. [DLMF §27.16 Cryptography, NIST Digital Library of Mathematical Functions](https://dlmf.nist.gov/27.16)
7. [CSE 311 Spring 2020 Lecture 11, University of Washington](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture11.html)
8. [Modular Arithmetic, Discrete Mathematics, A-Warded](https://a-warded.org/open-book/university-discrete-mathematics/lesson-18)
9. [Modular Arithmetics before C.F. Gauss](https://ghostarchive.org/pdfarc/WcLBP)
10. [Carl Friedrich Gauss, Heinz Klaus Strick, MathShistory, University of St Andrews](https://mathshistory.st-andrews.ac.uk/Strick/gauss.pdf)
11. [A Source Book in Mathematics, Smith, page 131, Wikisource](https://en.wikisource.org/wiki/Page%3AA_Source_Book_in_Mathematics_%28Smith%29.pdf/131)
12. [operator module documentation, CPython, GitHub](https://github.com/python/cpython/blob/main/Doc/library/operator.rst)
13. [Cryptography course pages 2025–26, Department of Computer Science and Technology, University of Cambridge](https://www.cl.cam.ac.uk/teaching/2526/Crypto/)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/modulo

English original: https://www.edgepedia.org/modulo
