SICP:1.2.5 Greatest Common Divisors
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 Divisors | GCD 与 Euclid's Algorithm |
| Euclid's Algorithm | successive remainder reduction |
| Lamé's Theorem | Fibonacci 与 logarithmic growth |
| Exercise 1.20 | normal-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:

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

这会把 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:
(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)
<sup>k</sup> /
<sup>5</sup>
因此,steps 数量 k 随 n 的 logarithm(base 为
)增长。所以该 process 的 order of growth 是 
(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,并按原文位置嵌入。
gcdprocedure、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;必要处译作增长阶 |
参考资料
- Harold Abelson、Gerald Jay Sussman、Julie Sussman:SICP 第二版 MIT Press 页面
- SICP HTML full text
- MIT OCW 6.001 Readings
评论 (0)