# 最大公约数

两个整数的**最大公约数**（gcd）是能同时整除二者的最大正整数，记作 \( \gcd(a, b) \) 或简记为 \( (a, b) \)。<sup>[1](https://www.whitman.edu/mathematics/higher_math_online/section03.03.html)</sup> 例如，6 与 10 的公约数是 1 和 2，故 \( \gcd(6, 10) = 2 \)。<sup>[2](https://www.math.gordon.edu/~kcrisman/mat338/section-6.html)</sup> 这一概念属于系统数学最古老的研究对象之列：欧几里得（Euclid）的《几何原本》（*Elements*，约公元前 300 年）在第 7 卷中开启其对数论的论述，给出了求两个或三个数的“最大公度”的命题，其中描述的方法——欧几里得算法——被斯坦福大学计算机科学家、《计算机程序设计艺术》（*The Art of Computer Programming*）的作者高德纳（Donald E. Knuth）称为“留存至今最古老的非平凡算法”和“一切算法的老祖宗”。<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup><sup> • </sup><sup>[4](https://arxiv.org/abs/0902.2465)</sup><sup> • </sup><sup>[5](https://www.claymath.org/library/historical/euclid/book07.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 满足 \( d \mid a \) 且 \( d \mid b \) 的最大正整数 \( d \)；记作 \( \gcd(a, b) \) 或 \( (a, b) \)，<sup>[1](https://www.whitman.edu/mathematics/higher_math_online/section03.03.html)</sup> |
| 贝祖刻画 | 最大公约数是能用 \( ax + by \)（\( x, y \) 为整数）表示的最小正数<sup>[2](https://www.math.gordon.edu/~kcrisman/mat338/section-6.html)</sup> |
| 古典算法 | 欧几里得《几何原本》第 7 卷命题 1 与命题 2（约公元前 300 年）；基于对任意 \( k \) 有 \( \gcd(m - kn, n) = \gcd(m, n) \)<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup> |
| 二进制算法 | 由斯坦（Stein）于 1961 年提出；只用减法、奇偶检验与偶数折半，不用除法<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup> |
| 最坏情形 | 相邻的斐波那契数；运行时间为 \( \Theta(\log a) \)<sup>[6](https://courses.cs.duke.edu/fall99/cps130/lectures/lect03/node10.html)</sup> |
| 运行时间分析 | 传统上归功于加布里埃尔·拉梅（Gabriel Lamé）1844 年的论文，更早的工作有雷诺（Reynaud，1811）、莱热（Léger，1837）与芬克（Finck，1841）<sup>[7](https://www.sciencedirect.com/science/article/pii/S0315086084710317)</sup> |
| 现代实践 | GMP 用莱默（Lehmer）算法计算大整数 GCD，超过某一规模阈值后改用亚二次的半 GCD 方法，时间为 \( O(M(N) \log N) \)<sup>[8](https://gmplib.org/gmp-man-6.3.0.pdf)</sup> |

## 等价刻画

最大公约数有三种看似不同、实则指向同一个数的描述。<sup>[2](https://www.math.gordon.edu/~kcrisman/mat338/section-6.html)</sup>

1. 它是满足 \( d \mid a \) 且 \( d \mid b \) 的最大整数 \( d \)。
2. 它是对 \( a \) 与 \( b \) 反复施行除法、即欧几里得算法所得的数。
3. 它是能写成 \( ax + by \)（\( x, y \) 为某对整数）的最小正数。

第三种形式称为贝祖等式，它断言：对不全为零的整数 \( a \) 与 \( b \)，存在整数 \( u \) 与 \( v \) 使 \( \gcd(a, b) = au + bv \)。<sup>[9](https://mathworld.wolfram.com/BezoutsIdentity.html)</sup> 每种刻画各有其用：第一种用于证明关于整除性的命题，第二种用于实际计算数值，第三种用于除法不便处理的证明，因为它只涉及加法与乘法。<sup>[2](https://www.math.gordon.edu/~kcrisman/mat338/section-6.html)</sup> 扩展欧几里得算法在算出最大公约数本身的同时，也算出系数 \( x \) 与 \( y \)。<sup>[10](https://math.virginia.edu/graduate/exams/algebra/alg-prep-2-1.html)</sup>

## 欧几里得算法

欧几里得算法的原始形式采用递归的整数除法，其依据是对任意 \( k \) 有 \( \gcd(m - kn, n) = \gcd(m, n) \)：\( m \) 与 \( n \) 的公约数也整除 \( m - kn \) 与 \( n \)，反之亦然。<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup> 欧几里得本人在第 7 卷命题 1 中的表述是不断从较大数中减去较小数，并得出结论：若最后只剩一个单位，则原两数互素；命题 2 则求它们的最大公度。<sup>[5](https://www.claymath.org/library/historical/euclid/book07.html)</sup>

**最坏情形行为。** 相邻的斐波那契数是欧几里得算法的最坏输入：对 \( F_{k+1} \) 与 \( F_k \) 施行该算法，余数会回溯出斐波那契数列，对 \( F_{k+1} \) 与 \( F_k \) 恰好需要 \( k \) 步。<sup>[6](https://courses.cs.duke.edu/fall99/cps130/lectures/lect03/node10.html)</sup><sup> • </sup><sup>[11](https://usna.edu/Users/cs/wcbrown/courses/S23SI335/lec/l38/lec.html)</sup> 因此最坏运行时间是 \( a \) 的反斐波那契函数减 2，它介于 \( \log(a) - 2 \) 与 \( 2\log(a) - 1 \) 之间，概括为 \( \Theta(\log a) \)。<sup>[6](https://courses.cs.duke.edu/fall99/cps130/lectures/lect03/node10.html)</sup>

这一分析的历史比通常的归功更为久远。对运行时间的首次分析传统上归于加布里埃尔·拉梅 1844 年的论文，但滑铁卢大学计算机科学家杰弗里·沙利特（Jeffrey Shallit）在《数学史》（*Historia Mathematica*）上指出：早在 1811 年，安托万-安德烈-路易·雷诺（Antoine-André-Louis Reynaud）就给出了一个较弱的界；拉梅的基本结果埃米尔·莱热（Émile Léger）在 1837 年已知晓；而皮埃尔-约瑟夫-艾蒂安·芬克（Pierre-Joseph-Étienne Finck）在 1841 年沿不同思路给出了完整而有效的证明。<sup>[7](https://www.sciencedirect.com/science/article/pii/S0315086084710317)</sup>

## 二进制算法与现代方法

二进制最大公约数算法由约瑟夫·斯坦（Josef Stein）于 1961 年提出。它基于三个事实：若 \( m \) 与 \( n \) 均为偶数，则 \( \gcd(m, n) = 2\gcd(m/2, n/2) \)；若 \( m \) 为偶数而 \( n \) 为奇数，则 \( \gcd(m, n) = \gcd(m/2, n) \)；若二者均为奇数，则 \( m - n \) 为偶数且 \( \gcd(m, n) = \gcd(m - n, n) \)。该算法只依赖减法、奇偶检验与对偶数右移，无需除法，因而很适合二进制算术。<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup> 对输入 \( u \) 与 \( v \)，其运行时间为 \( O((\log_2 uv)^2) \) 个位操作。<sup>[12](https://xlinux.nist.gov/dads/HTML/binaryGCD.html)</sup> 布里吉特·瓦莱（Brigitte Vallée）等人 2007 年的论文证明了，理查德·布伦特（Richard Brent）1976 年分析二进制算法的模型中出现的极限分布存在且唯一——布伦特曾猜想该函数存在，高德纳也曾作此设想。<sup>[13](https://www.sciencedirect.com/science/article/pii/S157086670600027X)</sup>

**多精度计算。** GMP 库展示了古典方法在实践中如何组合使用。在规模较小时，它使用 \( O(N^2) \) 的二进制式 GCD，但二进制算法目前仅在输入不足 3 个机器字时使用；GMP 手册报告，迄今在所有尝试过的场合，二进制算法都比欧几里得算法快，因为隐含的商通常很小，商为 1、2、3 的情形占 67.7%。<sup>[8](https://gmplib.org/gmp-man-6.3.0.pdf)</sup> 对更大的输入，莱默算法基于这样的洞见：商序列的前导部分完全由输入的最高有效部分决定，从而使大部分计算可用单字算术完成；GMP 版本遵循图多尔·耶贝莱安（Tudor Jebelean）的双位莱默-欧几里得方案，渐近运行时间为 \( O(N^2) \)。<sup>[8](https://gmplib.org/gmp-man-6.3.0.pdf)</sup> 超过阈值 `GCD_DC_THRESHOLD` 后，GMP 改用半 GCD（HGCD）函数，这是莱默算法的分治推广，其运行时间与由此得到的 GCD 一样为 \( O(M(N) \log N) \)，其中 \( M(N) \) 是相乘两个 \( N \) 字长数所需的时间。<sup>[8](https://gmplib.org/gmp-man-6.3.0.pdf)</sup> 递归应用的半 GCD 计算两个大数的 gcd 比朴素欧几里得算法更快。<sup>[14](https://mathworld.wolfram.com/Half-GCD.html)</sup> 各种变体进一步扩展了这些方法：有已分析最坏情形行为的左移二进制算法，<sup>[15](https://www.sciencedirect.com/science/article/pii/S0747717184710303)</sup> 有在 100 至 1000 位十进制数字输入上比欧几里得算法和二进制算法都快的 k 元算法，<sup>[16](https://www.sciencedirect.com/science/article/pii/S0196677484710066)</sup> 有向多于两个整数之 gcd 的推广，<sup>[17](https://arxiv.org/abs/1407.6794)</sup> 还有基于二进制算法、深度为 \( O(n \log n) \)、辅助位为 \( O(n) \) 的 GCD 计算量子线路。<sup>[18](https://arxiv.org/abs/1304.7516)</sup>

## 整数之外

只要所需的结构存在，gcd 概念便可推广。若 \( K \) 是域，则多项式环 \( K[x] \) 是欧几里得整环，故对多项式 \( f, g \in K[x] \)，\( \gcd(f, g) \) 可用多项式除法通过欧几里得算法计算。<sup>[19](https://www.risc.jku.at/education/courses/ws2007/ca/chap-4-neu.pdf)</sup> 对域 \( F \) 上任意非零多项式 \( f(x) \) 与 \( g(x) \)，gcd 存在，且可表示为 \( f(x) \) 与 \( g(x) \) 的线性组合。<sup>[20](https://faculty.niu.edu/math_beachy/aaol/polynomials.shtml)</sup> 在唯一分解整环中，多项式各系数的 gcd 称为其容度，而多项式为本原多项式当且仅当此容度为 1。<sup>[21](https://math.ucla.edu/~sharifi/notes/algebra-ch05.html)</sup> 多项式 gcd 在 \( \mathbb{Z}[x] \) 上也有良好定义，后者是唯一分解整环，虽非欧几里得整环。<sup>[22](https://www.csd.uwo.ca/~mmorenom/CS424/Lectures/ResultantGcd.html/node2.html)</sup>

## 应用

gcd 在现代最突出的用途是模算术与密码学。整数 \( m \) 模 \( n \) 的乘法逆元存在，当且仅当 \( m \) 与 \( n \) 互素，即 \( \gcd(m, n) = 1 \)；该逆元可由扩展欧几里得算法求得，因为把 \( \gcd \) 写成线性组合 \( mr + ns = 1 \) 便给出 \( mr \equiv 1 \pmod n \)。<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup><sup> • </sup><sup>[23](https://cp-algorithms.com/algebra/module-inverse.html)</sup> 计算模逆元的算法可追溯至北印度的阿耶波多（Aryabhata，公元 499 年）。<sup>[3](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)</sup> GCD 计算也服务于有理数算术，用于约化分子与分母。<sup>[17](https://arxiv.org/abs/1407.6794)</sup>

这一思想在希腊之外独立产生。一项关于中国古代数学的研究认为，中国古时用于求最大公约数的算法本质上就是欧几里得算法或二进制 gcd 算法，且中国的素数概念早于《几何原本》。<sup>[24](https://arxiv.org/abs/0910.0095)</sup> 欧几里得算法本身蕴含他的第一定理——算术基本定理唯一性部分的核心——以及他的第二定理：素数有无穷多个；这正是它被视为欧几里得所构建的数论之基石的部分原因。<sup>[4](https://arxiv.org/abs/0902.2465)</sup>

## References

1. [The Euclidean Algorithm, §3.3, Whitman College, *Higher Math*](https://www.whitman.edu/mathematics/higher_math_online/section03.03.html)
2. [Section 2.2 The GCD, Gordon College (Math 338 notes)](https://www.math.gordon.edu/~kcrisman/mat338/section-6.html)
3. [Euclid's Method for Finding the Greatest Common Divisor (SMILI TR-2001-95, Princeton)](https://www.princeton.edu/~rblee/ELE572Papers/ECCpapers/Euclid_2_Divide_smli_tr-2001-95.pdf)
4. [Shaohua Zhang (2009). Euclid's Number-Theoretical Work. arXiv.](https://arxiv.org/abs/0902.2465)
5. [Euclid's Elements, Book VII, Clay Mathematics Institute Historical Archive](https://www.claymath.org/library/historical/euclid/book07.html)
6. [Summary, CPS 130 Lecture 3, Duke University](https://courses.cs.duke.edu/fall99/cps130/lectures/lect03/node10.html)
7. [Jeffrey Shallit (1994). Origins of the analysis of the Euclidean algorithm. *Historia Mathematica* 21(4), 401–419.](https://www.sciencedirect.com/science/article/pii/S0315086084710317)
8. [GNU MP 6.3.0 Manual, Chapter 15: Algorithms](https://gmplib.org/gmp-man-6.3.0.pdf)
9. [Bézout's Identity, Wolfram MathWorld](https://mathworld.wolfram.com/BezoutsIdentity.html)
10. [Algebra General Exam Review, University of Virginia Mathematics](https://math.virginia.edu/graduate/exams/algebra/alg-prep-2-1.html)
11. [SI335: Number theoretic computations, United States Naval Academy](https://usna.edu/Users/cs/wcbrown/courses/S23SI335/lec/l38/lec.html)
12. [Binary GCD, Dictionary of Algorithms and Data Structures, NIST](https://xlinux.nist.gov/dads/HTML/binaryGCD.html)
13. [Brigitte Vallée and others (2007). Existence of a limiting distribution for the binary GCD algorithm. *Journal of Discrete Algorithms* 5(1), 176–186.](https://www.sciencedirect.com/science/article/pii/S157086670600027X)
14. [Half-GCD, Wolfram MathWorld](https://mathworld.wolfram.com/Half-GCD.html)
15. [Jeffrey Shallit, Jonathan Sorenson (1994). Analysis of a Left-Shift Binary GCD Algorithm. *Journal of Symbolic Computation* 17(6), 473–486.](https://www.sciencedirect.com/science/article/pii/S0747717184710303)
16. [Two Fast GCD Algorithms, *Journal of Algorithms*](https://www.sciencedirect.com/science/article/pii/S0196677484710066)
17. [GCD Computation of n Integers. arXiv.](https://arxiv.org/abs/1407.6794)
18. [Quantum Circuits for GCD Computation with O(n log n) Depth and O(n) Ancillae. arXiv.](https://arxiv.org/abs/1304.7516)
19. [Greatest common divisors of polynomials, RISC course notes, JKU Linz](https://www.risc.jku.at/education/courses/ws2007/ca/chap-4-neu.pdf)
20. [Polynomials, Northern Illinois University, Abstract Algebra On Line](https://faculty.niu.edu/math_beachy/aaol/polynomials.shtml)
21. [Romyar Sharifi. Abstract Algebra, Chapter 5: Advanced ring theory, UCLA](https://math.ucla.edu/~sharifi/notes/algebra-ch05.html)
22. [Relation between gcds in Z[x] and K[x]; Polynomial gcds in non Euclidean domains, CS424 notes, University of Western Ontario](https://www.csd.uwo.ca/~mmorenom/CS424/Lectures/ResultantGcd.html/node2.html)
23. [Modular Inverse, Algorithms for Competitive Programming](https://cp-algorithms.com/algebra/module-inverse.html)
24. [The concept of primes and the algorithm for counting the greatest common divisor in Ancient China. arXiv.](https://arxiv.org/abs/0910.0095)

---
*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 › Divisibility, GCD, and the integers

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/greatest-common-divisor

English original: https://www.edgepedia.org/greatest-common-divisor
