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

SICP:1.2.4 Exponentiation

发布于 2026-09-18 08:12 · 最后编辑于 2026-09-18 08:41 · 版本 v2 · 字数 4,471 · 预计阅读 9 分钟 👁 47 次阅读

SICP:1.2.4 Exponentiation

本文逐段翻译 SICP 第二版 §1.2.4《Exponentiation》,保留 recursive、iterative 与 successive-squaring algorithms、公式图件、复杂度分析以及 Exercises 1.16–1.19 的原文顺序。

TL;DR

  • 直接递归计算 bⁿ 是 linear recursive process;加入 accumulator 后可以变成 linear iteration,并在 constant space 中运行。
  • Successive squaring 利用 exponent 的 parity,把 exponentiation 的 steps 和 space 都降为 logarithmic growth。
  • Exercises 1.16–1.19 将 successive squaring 的 idea 推广到 iterative exponentiation、multiplication 和 logarithmic-time Fibonacci。

目录

章节说明
1.2.4 Exponentiationexponentiation 的递归定义
Linear recursive 与 iterativeexpt 的两种基础实现
Successive squaringfast-expt 与 logarithmic growth
Exercises 1.16–1.19iterative algorithms 与 Fibonacci transformation

1.2.4 Exponentiation

考虑计算给定 number 的 exponential 这一问题。我们希望有一个 procedure,接受 base b 和 positive integer exponent n 作为 arguments,并计算 bⁿ。

Linear recursive 与 iterative

一种方式是使用 recursive definition:

ch1 Z G 20

这个 definition 很容易翻译成 procedure:

scheme
(define (expt b n)
  (if (= n 0)
      1
      (* b (expt b (- n 1)))))

这是一个 linear recursive process,需要 book Z G D 3(n) steps 和 book Z G D 3(n) space。和 factorial 一样,我们可以很容易地 formulate 一个 equivalent linear iteration:

scheme
(define (expt b n)
  (expt-iter b n 1))

(define (expt-iter b counter product)
  (if (= counter 0)
      product
      (expt-iter b
                 (- counter 1)
                 (* b product))))

这个 version 需要 book Z G D 3(n) steps 和 book Z G D 3(1) space。

Successive squaring

通过 successive squaring,我们可以用更少的 steps 计算 exponentials。例如,与其通过以下方式计算 b⁸:

ch1 Z G 21

不如使用三次 multiplications:

ch1 Z G 22

对于 exponents 是 powers of 2 的情况,这个 method 工作良好。对于一般的 exponents,如果使用以下 rule,同样可以利用 successive squaring:

ch1 Z G 23

我们可以把这个 method 表达为 procedure:

scheme
(define (fast-expt b n)
  (cond ((= n 0) 1)
        ((even? n) (square (fast-expt b (/ n 2))))
        (else (* b (fast-expt b (- n 1))))))

其中,用来测试 integer 是否 even 的 predicate,依据 primitive procedure remainder 定义为:

scheme
(define (even? n)
  (= (remainder n 2) 0))

fast-expt 演化出的 process,在 space 和 steps 数量两个方面都随 n logarithmically growth。要理解这一点,观察使用 fast-expt 计算 b²ⁿ,只比计算 bⁿ 多一次 multiplication。于是,我们能够计算的 exponent size,大约会随着允许增加的每一次 multiplication 而 double。因此,exponent 为 n 时所需 multiplications 的数量,大约以 log₂ n 的速度增长。该 process 的 growth 是 book Z G D 3(log n)1。

当 n 变大时,book Z G D 3(log n) growth 与 book Z G D 3(n) growth 的 difference 会非常 striking。例如,n = 1000 时,fast-expt 只需 14 次 multiplications2。还可以利用 successive squaring 的 idea,设计一个用 logarithmic steps 计算 exponentials 的 iterative algorithm(见 Exercise 1.16),不过和通常情况一样,这个 iterative algorithm 写起来没有 recursive algorithm 那么 straightforward3。

Exercises 1.16–1.19

Exercise 1.16

设计一个 procedure,使其演化出使用 successive squaring、steps 数量为 logarithmic 的 iterative exponentiation process,就像 fast-expt 一样。

提示:使用 observation (bⁿ⁄²)² = (b²)ⁿ⁄²。除了 exponent n 与 base b 外,再维护一个 additional state variable a,并定义 state transformation,使 product a bⁿ 在各个 states 之间保持不变。Process 开始时令 a 为 1,process 结束时 a 的 value 就是 answer。一般而言,定义一个从 state 到 state 始终不变的 invariant quantity,是思考 iterative algorithm design 的 powerful way。

Exercise 1.17

本节的 exponentiation algorithms 通过 repeated multiplication 执行 exponentiation。同样地,可以通过 repeated addition 执行 integer multiplication。下面的 multiplication procedure(假设 language 只能 add,不能 multiply)与 expt procedure analogous:

scheme
(define (* a b)
  (if (= b 0)
      0
      (+ a (* a (- b 1)))))

这个 algorithm 所需 steps 数量与 b linear。现在,假设除 addition 外,我们还提供 double(把 integer double)和 halve(将 even integer 除以 2)operations。使用它们设计一个与 fast-expt analogous、steps 数量为 logarithmic 的 multiplication procedure。

Exercise 1.18

利用 Exercises 1.16 和 1.17 的 results,设计一个 procedure,使用 adding、doubling 和 halving,以 iterative process 计算两个 integers 的 multiplication,并使用 logarithmic number of steps4。

Exercise 1.19

有一个 clever algorithm,可以用 logarithmic number of steps 计算 Fibonacci numbers。

回顾 §1.2.2 的 fib-iter process 中 state variables a 与 b 的 transformation:a ← a + b,b ← a。把这个 transformation 称为 T,并注意从 1 和 0 开始反复应用 T n 次,会产生 pair Fib(n + 1) 和 Fib(n)。换句话说,Fibonacci numbers 是从 pair (1, 0) 开始,通过应用 Tⁿ(transformation T 的 nth power)产生的。

现在把 T 看作 transformations family T_pq 中 p = 0、q = 1 的 special case,其中 T_pq 按照以下 rule 把 pair (a, b) transform:

text
a ← bq + aq + ap
b ← bp + aq

证明:将 transformation T_pq 应用两次,其 effect 等同于使用同一形式的 single transformation T_p′q′;并用 p 和 q 计算 p′、q′。这样就得到一种显式 squaring transformations 的方式,因此可以像 fast-expt 一样通过 successive squaring 计算 Tⁿ。把这些内容整合起来,完成下面这个 steps 数量为 logarithmic 的 procedure:

scheme
(define (fib n)
  (fib-iter 1 0 0 1 n))

(define (fib-iter a b p q count)
  (cond ((= count 0) b)
        ((even? count)
         (fib-iter a
                   b
                   <??>        ; compute p'
                   <??>        ; compute q'
                   (/ count 2)))
        (else (fib-iter (+ (* b q) (* a q) (* a p))
                        (+ (* b p) (* a q))
                        p
                        q
                        (- count 1)))))

图与格式复核

  • exponentiation definition、b⁸ 的 successive-squaring 对比与 general rule 均保留原始 GIF/PNG,位置与原文一致。
  • expt、expt-iter、fast-expt、Exercises 1.16–1.19 的 Scheme code 顺序保持不变。
  • 图件原始尺寸:84×34、184×15、75×54、186×34,以及 Θ/Growth 符号 9×10。

术语对照

English译法/处理
exponentiation保留 exponentiation;必要处译作幂运算
base / exponent保留 base、exponent;必要处译作底数、指数
successive squaring保留 successive squaring;必要处译作连续平方
invariant quantity保留 invariant quantity;必要处译作不变量
logarithmic growth保留 logarithmic growth;必要处译作对数增长
transformation保留 transformation;必要处译作变换
Russian peasant method保留 Russian peasant method

参考资料

  1. 更 precisely 地说,所需 multiplications 数量等于 log₂ n 减 1,再加上 n 的 binary representation 中 1 的数量;这个总数总是小于 2 log₂ n。Order notation 定义中的 arbitrary constants k₁、k₂ 意味着,对 logarithmic process 而言,logarithm 的 base 并不重要,因此这类 processes 都记作 book Z G D 3(log n)。 ↩

  2. 原文脚注提示,关于为什么有人会关心把 numbers raise 到 1000th power,参见 §1.2.6。 ↩

  3. 这个 iterative algorithm 很 ancient,出现在公元前 200 年以前成书的 Ācārya Piṅgala《Chandaḥ-sūtra》中;Knuth 1981 §4.6.3 对此及其他 exponentiation methods 有完整 discussion 与 analysis。 ↩

  4. 这个有时被称为 “Russian peasant method” 的 algorithm 很 ancient;最早的 examples 出现在 Rhind Papyrus 中,这是现存最古老的两份 mathematical documents 之一,约公元前 1700 年由 Egyptian scribe A'h-mose 抄写。 ↩

← 返回列表
← 上一篇 SICP:1.2.3 Orders of Growth 下一篇 → SICP:1.2.5 Greatest Common Divisors

评论 (0)

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