# 余数

**余数**是当一个整数（被除数）除以另一个整数（除数），并且只减去除数的整数倍后，剩下的整数。用 3 除 14，得到四个完整组，剩下 2，写作 14 = 3 · 4 + 2，所以余数是 2。<sup>[1](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter4/div-mod.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义定理 | 对整数 \( b \) 和非零整数 \( a \)，存在唯一的整数 \( q \) 和 \( r \)，使得 \( b = aq + r \) 且 \( 0 \leq r < a \)。<sup>[1](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter4/div-mod.html)</sup> |
| 名称 | \( q \) 是商，\( r \) 是余数；返回这两个数的运算通常写作 div 和 mod。<sup>[1](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter4/div-mod.html)</sup> |
| 示例 | \( 14 = 3 \cdot 4 + 2 \)：3 除 14，商为 4，余数为 2。<sup>[1](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter4/div-mod.html)</sup> |
| 历史作用 | 欧几里得（Euclid）在《几何原本》中以他的算法开启其数论，该算法本质上等价于贝祖（Bézout）等式和除法算法；高斯（Gauss）在《算术研究》（1801）中把同余理论建立在余数之上。<sup>[2](https://arxiv.org/abs/0902.2465)</sup> |
| 计算最大公约数 | 反复进行带余除法，直到余数为零为止，这就是求最大公约数的欧几里得算法。<sup>[3](https://math.oxford.emory.edu/site/math125/euclideanAlgorithm/)</sup> |
| 应用 | 余数驱动模算术，而模算术是公钥密码学、校验和与校验位、哈希表以及日历周期的基础。<sup>[4](https://www.yijiansuan.com/en/guide/modular-guide)</sup> |

## 除法定理

余数的存在性与唯一性是一个定理，通常称为除法算法或欧几里得除法，尽管它本身并不是一串步骤。<sup>[5](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)</sup> 其标准形式为：设 \( a, b \) 为整数且 \( a \neq 0 \)；则存在唯一的整数 \( q \) 和 \( r \)，使得

\[ b = aq + r, \quad 0 \leq r < a. \]

条件 \( 0 \leq r < a \) 正是使这对数唯一的原因：如果不对 \( r \) 加以限制，同一次除法可以写成许多种形式，例如 \( 14 = 3 \cdot 3 + 5 \) 以及 \( 14 = 3 \cdot 4 + 2 \)。<sup>[1](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter4/div-mod.html)</sup> 一个标准证明考虑可表示为 \( n - qd \) 的非负整数集合，并取其中最小的元素；如果该元素至少为 \( d \)，那么减去 \( d \) 就会产生一个更小的元素，矛盾。<sup>[6](https://www.math.umd.edu/~pbrosnan/notes/ugalg/sect0005.html)</sup>

商通常写作 \( \lfloor n/m \rfloor \)，即 \( n/m \) 的下取整，以明确所指为整数商，而余数则为 \( n \bmod m \)。<sup>[7](https://cs.yale.edu/homes/aspnes/pinewiki/NumberTheory.html)</sup>

## 欧几里得算法

**反复取余可求出最大公约数。** 用 \( n \) 除 \( m \) 得到唯一的商和余数；对新的一对数施加同样的过程，会得到更小的一对数，如此继续，直到某次除法的余数为零，过程终止。<sup>[3](https://math.oxford.emory.edu/site/math125/euclideanAlgorithm/)</sup> 这就是欧几里得算法，它计算这两个数的最大公约数。沿途记录额外信息，就得到扩展欧几里得算法，它还能找出整数系数，把最大公约数表示为原来两个数的线性组合。<sup>[5](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)</sup>

该算法的历史分量相当重。欧几里得在《几何原本》的数论各卷中以这个算法开篇，而不是以除法开篇；它本质上等价于贝祖等式和除法算法；它还蕴含欧几里得第一定理——算术基本定理唯一性部分的核心——以及欧几里得第二定理，即素数有无穷多个。唐纳德·克努特（Donald Knuth）称欧几里得的方法为“所有算法的老祖宗”。<sup>[2](https://arxiv.org/abs/0902.2465)</sup>

## 模算术

余数定义同余：两个整数模 \( m \) 同余，是指它们除以 \( m \) 后余数相同。模算术可以简单地看作求一个数除以另一个数时的余数。<sup>[8](https://education.casio.co.uk/article-modular-arithmetic-explained-basics-applications-and-examples/)</sup> 由除法算法，对每个 \( n \) 和 \( m \)，存在唯一的余数 \( r \)，满足 \( 0 \leq r < m \) 且 \( n = qm + r \)，这个唯一的 \( r \) 写作 \( n \bmod m \)。<sup>[7](https://cs.yale.edu/homes/aspnes/pinewiki/NumberTheory.html)</sup>

余数算术有广泛的实用范围。它在密码学中起核心作用，加密和解密算法利用模运算来保护数据并生成安全密钥。<sup>[8](https://education.casio.co.uk/article-modular-arithmetic-explained-basics-applications-and-examples/)</sup> 它还驱动星期周期、校验和——例如身份证的最后一位（GB 11643 标准中的模 11 加权和）——以及哈希表。<sup>[4](https://www.yijiansuan.com/en/guide/modular-guide)</sup> 在 ISBN 方案中，前九位数字承载出版数据，第十位数字是用于查错的冗余校验。<sup>[9](https://www.cs.cornell.edu/~rafael/discmath.pdf)</sup> 一个有用的代数事实是：幂的余数只取决于底数的余数：如果一个数写作 \( mq + r \)，那么 \( (mq+r)^2 = m(mq^2 + 2qr) + r^2 \)，因此平方的最终余数仅由 \( r^2 \) 决定。<sup>[10](https://www.math.gordon.edu/~kcrisman/mat338/section-5.html)</sup>

## 编程中的余数

**各语言对负被除数的处理并不一致。** Java 的语言规范对浮点操作数上的 `%` 的定义，类似于整数余数运算符，隐含的除法采用向零舍入，可比于 C 库函数 `fmod`；\( n \) 除以 \( d \) 的浮点余数 \( r \) 满足 \( r = n - d \cdot q \)，其中 \( q \) 的绝对值尽可能大，但不超过真实商的绝对值。<sup>[11](https://download.java.net/java/early_access/jdk27/docs/specs/jls/jls-15.html)</sup> 实际后果是，用负值进行哈希或索引的代码，在依赖结果之前必须了解该语言的约定。<sup>[4](https://www.yijiansuan.com/en/guide/modular-guide)</sup>

## 文化与计算扩展

在戈德弗里德·图桑（Godfried Toussaint）2005 年对世界音乐中的欧几里得节奏进行考察之后，欧几里得算法的一种修改形式在音乐作曲家中流行起来；一种非递归矩阵构造只使用模算术和组合数学，尽管它并不优于传统的带余除法。<sup>[12](https://arxiv.org/abs/2206.12421)</sup> 在计算机算术中，巴雷特型算法仅使用模算术，以剩余形式完成整个计算，而不通过中国剩余定理转换回大整数，并着眼于密码学应用。<sup>[13](https://arxiv.org/abs/1602.01551)</sup>

## References

1. [Divisibility and Modular Arithmetic, Discrete Structures for Computing (A. Brandt, Western University)](https://www.csd.uwo.ca/~abrandt5/teaching/DiscreteStructures/Chapter4/div-mod.html)
2. [Shaohua Zhang, Euclid's Number-Theoretical Work, arXiv:0902.2465](https://arxiv.org/abs/0902.2465)
3. [The Euclidean Algorithm, Emory University Math 125 materials](https://math.oxford.emory.edu/site/math125/euclideanAlgorithm/)
4. [Modular Arithmetic: Remainders, Rules & Applications, Yijian Suan guide](https://www.yijiansuan.com/en/guide/modular-guide)
5. [21-110: The extended Euclidean algorithm, Carnegie Mellon University](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)
6. [UMD 403: The division algorithm with remainder, University of Maryland](https://www.math.umd.edu/~pbrosnan/notes/ugalg/sect0005.html)
7. [NumberTheory, James Aspnes, Yale University](https://cs.yale.edu/homes/aspnes/pinewiki/NumberTheory.html)
8. [Modular arithmetic explained: basics, applications and examples, Casio Education](https://education.casio.co.uk/article-modular-arithmetic-explained-basics-applications-and-examples/)
9. [Modular Arithmetic, Cornell CS discrete mathematics notes](https://www.cs.cornell.edu/~rafael/discmath.pdf)
10. [NTIC: The Division Algorithm, Gordon College](https://www.math.gordon.edu/~kcrisman/mat338/section-5.html)
11. [Chapter 15. Expressions, Java Language Specification, JDK 27](https://download.java.net/java/early_access/jdk27/docs/specs/jls/jls-15.html)
12. [On The Euclidean Algorithm: Rhythm Without Recursion, arXiv:2206.12421](https://arxiv.org/abs/2206.12421)
13. [New Residue Arithmetic Based Barrett Algorithms, Part I, arXiv:1602.01551](https://arxiv.org/abs/1602.01551)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Elementary number theory › Congruences and modular arithmetic

Copyright 2026 Edgepedia

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

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

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