目录
正在加载目录…
正在阅读专栏 SICP:Structure and Interpretation of Computer Programs 第 13 篇 · 共 80 篇 · 16%

SICP:1.2.5 Greatest Common Divisors

发布于 2026-09-18 08:12 · 最后编辑于 2026-09-18 08:43 · 版本 v2 · 字数 3,121 · 预计阅读 7 分钟 👁 38 次阅读

SICP:1.2.5 Greatest Common Divisors

本文逐段翻译 SICP 第二版 §1.2.5《Greatest Common Divisors》,保留 Euclid's Algorithm、remainder reduction、Lamé's Theorem、复杂度分析、公式图件以及 Exercise 1.20 的原文顺序。

TL;DR

  • GCD 是同时整除两个 integers 且无 remainder 的最大 integer;Euclid's Algorithm 通过不断替换 (a, b) 为 (b, remainder(a, b)) 高效计算 GCD。
  • 这个 procedure 生成 iterative process,steps 数量具有 logarithmic growth。
  • Lamé's Theorem 把 Euclid's Algorithm 的 steps 与 Fibonacci numbers 联系起来,从而给出其 Θ(log n) 的复杂度界。

目录

章节说明
1.2.5 Greatest Common DivisorsGCD 与 Euclid's Algorithm
Euclid's Algorithmsuccessive remainder reduction
Lamé's TheoremFibonacci 与 logarithmic growth
Exercise 1.20normal-order 与 applicative-order 的比较

1.2.5 Greatest Common Divisors

两个 integers a 和 b 的 greatest common divisor(GCD),定义为能够同时整除 a 和 b 且没有 remainder 的最大 integer。例如,16 和 28 的 GCD 是 4。

在 Chapter 2 研究如何实现 rational-number arithmetic 时,我们需要能够计算 GCD,以便将 rational numbers reduction 到 lowest terms。(要把 rational number reduction 到 lowest terms,必须用它们的 GCD 同时除 numerator 和 denominator。例如,16/28 reduction 后是 4/7。)

寻找两个 integers 的 GCD,一种方法是 factorize 它们,再搜索 common factors;但有一个 famous algorithm 要 efficient 得多。

Euclid's Algorithm

该 algorithm 的 idea 建立在以下 observation 上:如果 r 是 a 除以 b 所得的 remainder,那么 a 与 b 的 common divisors,恰好与 b 与 r 的 common divisors 相同。因此,可以使用以下 equation:

ch1 Z G 24

不断使用它,就能把计算 GCD 的 problem successive reduction 为计算越来越小的 integer pairs 的 GCD。

例如:

ch1 Z G 25

这会把 GCD(206, 40) reduction 为 GCD(2, 0),其结果是 2。可以证明,从任意两个 positive integers 开始,反复进行 reductions,最终总会得到一个 second number 为 0 的 pair。此时,GCD 就是 pair 中的另一个 number。这种计算 GCD 的 method 称为 Euclid's Algorithm1。

很容易把 Euclid's Algorithm 表达为 procedure:

scheme
(define (gcd a b)
  (if (= b 0)
      a
      (gcd b (remainder a b))))

这个 procedure 生成一个 iterative process,其 steps 数量随 involved numbers 的 logarithm 增长。

Lamé's Theorem

Euclid's Algorithm 所需 steps 数量具有 logarithmic growth,这一事实与 Fibonacci numbers 存在 interesting relation:

Lamé's Theorem: 如果 Euclid's Algorithm 需要 k steps 来计算某个 pair 的 GCD,那么该 pair 中较小的 number 必须大于或等于第 k 个 Fibonacci number2。

可以用这个 theorem 得到 Euclid's Algorithm 的 order-of-growth estimate。令 n 为 procedure 两个 inputs 中较小的那个。如果 process 需要 k steps,那么必须有:

n ≥ Fib(k) book Z G D 20 book Z G D 11<sup>k</sup> / book Z G D 13<sup>5</sup>

因此,steps 数量 k 随 n 的 logarithm(base 为 book Z G D 11)增长。所以该 process 的 order of growth 是 book Z G D 3(log n)。

Exercise 1.20

procedure 生成的 process,当然取决于 interpreter 使用的 rules。作为例子,考虑上面给出的 iterative gcd procedure。

假设按照 §1.1.5 的描述,使用 normal-order evaluation 来 interpret 这个 procedure。(if 的 normal-order-evaluation rule 在 Exercise 1.5 中描述。)使用 substitution method(针对 normal order),说明 evaluation (gcd 206 40) 时生成的 process,并指出实际执行的 remainder operations。

在 (gcd 206 40) 的 normal-order evaluation 中,实际执行了多少次 remainder operation?在 applicative-order evaluation 中呢?

图与格式复核

  • GCD reduction equation 原始 GIF 尺寸为 141 × 15;GCD(206, 40) reduction 图尺寸为 183 × 94。
  • Lamé growth 中的 Fibonacci、golden-ratio 与 Θ 符号图件均保留原始 GIF,并按原文位置嵌入。
  • gcd procedure、remainder process 与 Exercise 1.20 的要求保持原文顺序。

术语对照

English译法/处理
greatest common divisor保留 GCD / greatest common divisor;必要处译作最大公约数
common divisor保留 common divisor;必要处译作公约数
remainder保留 remainder;必要处译作余数
Euclid's Algorithm保留 Euclid's Algorithm;必要处译作欧几里得算法
Lamé's Theorem保留 Lamé's Theorem;必要处译作拉梅定理
nontrivial algorithm保留 nontrivial algorithm
order of growth保留 order of growth;必要处译作增长阶

参考资料

  1. Euclid's Algorithm 之所以得名,是因为它出现在 Euclid 的《Elements》(Book 7,约公元前 300 年)。根据 Knuth(1973),它可以被视为已知最古老的 nontrivial algorithm。古埃及 multiplication method(Exercise 1.18)当然更古老,但 Euclid's Algorithm 是已知最早被作为 general algorithm、而非 illustrative examples 提出的 algorithm。 ↩

  2. 该 theorem 由 Gabriel Lamé 于 1845 年证明。 ↩

← 返回列表
← 上一篇 SICP:1.2.4 Exponentiation 下一篇 → SICP:1.2.6 Example: Testing for Primality

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论