目录
正在加载目录…
正在阅读专栏 Algorithms:CLRS + MIT 6.006 第 11 篇 · 共 24 篇 · 46%

Integer Arithmetic、Newton 与 Karatsuba Multiplication

发布于 2026-09-18 18:34 · 最后编辑于 2026-09-18 18:34 · 版本 v2 · 字数 4,857 · 预计阅读 10 分钟 👁 20 次阅读

Algorithms:Integer Arithmetic and Karatsuba Multiplication

本文逐段翻译 MIT 6.006 Fall 2011 Lecture 11《Numerics I》,保留原讲义从 irrational numbers、Catalan numbers 到 Newton’s method、高精度计算和 Karatsuba multiplication 的完整展开;核心主题是如何用分治把 high-precision multiplication 从 Θ(n²) 降到 Θ(n^{log₂3})。

TL;DR

  • Motivation:irrational numbers 的 decimal expansion 没有显然的周期模式;高精度计算需要处理比 machine word 大得多的 numbers。
  • Newton’s method:对 f(x)=x²-a,迭代 x_{i+1}=1/2(x_i+a/x_i),可以 quadratic convergence;正确的 division 是实现高精度 square root 的下一步。
  • Schoolbook multiplication:把两个 n-digit numbers 各拆成 high / low halves,需要 4 次 half-size multiplication,得到 T(n)=4T(n/2)+Θ(n)=Θ(n²)。
  • Karatsuba:用 (x₀+x₁)(y₀+y₁) 一次性得到 cross term,只需 3 次 half-size multiplication,T(n)=3T(n/2)+Θ(n)=Θ(n^{log₂3})≈Θ(n^{1.585})。
  • 应用:high-precision arithmetic 可以计算极高精度的几何量;讲义用 chord problem 展示 Catalan numbers 如何出现在一个看似无关的平方根展开中。

目录

章节说明
Lecture 11:Numerics Iirrational numbers 与高精度计算的动机
Catalan numbers 的 digressionbalanced parentheses 与递推
Newton’s methodtangent approximation 与 square roots
High-precision computation用 integers 表示 decimal digits
High-precision multiplicationschoolbook multiplication 的 recurrence
Karatsuba’s method3 次递归乘法的分治优化
几何例子与 Catalan numbers高精度结果中的隐藏结构
复杂度总结multiplication 与 Newton iteration
小结本讲要点

Lecture 11:Numerics I

本讲从 irrational numbers 开始。Pythagoras 发现 square 的 diagonal 与 side 是 incommensurable 的:它们不能表示为两个 integers 的 ratio。对 unit square,diagonal 是:

text
        1
    ┌───────┐
  1 │  √2   │
    └───────┘

Pythagoras 的学派崇尚 “All is number”,所以 irrational numbers 被视为一种威胁。讲义提出一个 motivating question:

text
irrational numbers 的 decimal expansions 中,是否隐藏着某种 pattern?

例如:

text
√2 = 1.414 213 562 373 095
     048 801 688 724 209
     698 078 569 671 875 ...

要观察百万位甚至更多 digits,必须能够进行 high-precision arithmetic,而不能只依赖 fixed-width machine numbers。

Catalan numbers 的 digression

在进入 Newton’s method 之前,讲义插入了一个关于 Catalan numbers 的 digression。

Balanced parentheses

令 P 是 balanced-parentheses strings 的 set。它递归定义为:

text
1. 空 string λ 属于 P;
2. 如果 α、β 属于 P,那么 (α)β 也属于 P。

每一个 nonempty balanced-parentheses string 都能由唯一的一对 α, β 按规则 2 构造。例如:

text
(())() = ( ( ) ) ( )
  α       β

Catalan recurrence

令 C_n 表示恰好包含 n 对 parentheses 的 balanced strings 数量。显然:

text
C₀ = 1

一个包含 n+1 对 parentheses 的 string,最外层显式贡献一对;设 α 包含 k 对,β 包含 n-k 对,就得到:

text
C_{n+1} = ∑_{k=0}^n C_k · C_{n-k}

前几项是:

text
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, ...

这些就是 Catalan numbers。它们会在后面的 power-series 解释中重新出现。

Newton’s method

Newton’s method 用 successive approximation 求方程 f(x)=0 的 root。假设当前 approximation 是 x_i,在点 (x_i, f(x_i)) 处的 tangent line 是:

text
y = f(x_i) + f'(x_i) · (x - x_i)

令这条 tangent 与 x-axis 的交点为下一次 approximation:

text
x_{i+1} = x_i - f(x_i) / f'(x_i)

每一次 iteration 都用当前 point 的 tangent 代替原函数;在 root 附近,误差通常会 quadratic 收敛。

Square roots

要计算 √a,令:

text
f(x) = x² - a
f'(x) = 2x

代入 Newton iteration:

text
x_{i+1}
  = x_i - (x_i² - a)/(2x_i)
  = 1/2 · (x_i + a/x_i)

也就是不断取当前 guess x_i 与 a/x_i 的 arithmetic mean。

示例:√2

从 x₀=1 开始:

text
x₀ = 1.000000000
x₁ = 1.500000000
x₂ = 1.416666666
x₃ = 1.414215686
x₄ = 1.414213562

可以看到,正确 digits 大致每一步翻倍,这就是 quadratic convergence。讲义同时指出:要使用 Newton’s method,就需要 high-precision division;本讲先解决 multiplication,division 留到 Lecture 12。

High-precision computation

如果需要 √2 的 d 位 decimal precision,可以把目标转化为整数:

text
√2 = 1.414213562373...

希望计算:

text
floor(10^d · √2)

还可以写成:

text
floor( √(2 · 10^{2d}) )

因此,high-precision square root 可以继续使用 Newton’s method;关键是需要处理拥有 d 位或更多 digits 的 integers。

在下面的 multiplication analysis 中,n 表示 radix r 下的 digit 数量。r 可以是 2、10 或其他适合实现的 radix。

High-precision multiplication

考虑两个 n-digit numbers x 和 y,其中:

text
0 ≤ x, y < r^n

把每个 number 拆成 high half 和 low half:

text
x = x₁ · r^{n/2} + x₀
y = y₁ · r^{n/2} + y₀

其中:

text
x₁、y₁:high halves
x₀、y₀:low halves

每个 half 大约有 n/2 digits。展开乘积:

text
z = x · y
  = x₁y₁ · r^n
    + (x₀y₁ + x₁y₀) · r^{n/2}
    + x₀y₀

Schoolbook divide-and-conquer

直接分别计算:

text
x₁y₁
x₀y₁
x₁y₀
x₀y₀

需要 4 次 half-sized multiplication。加法、移位和组合只需要 Θ(n):

text
T(n) = 4T(n/2) + Θ(n)

由 recursion-tree 或 Master theorem:

text
T(n) = Θ(n^{log₂4}) = Θ(n²)

这和普通 schoolbook multiplication 的 quadratic bound 一致。

Karatsuba’s method

观察 cross term:

text
x₀y₁ + x₁y₀

不必分别计算 x₀y₁ 和 x₁y₀。先计算三个 products:

text
z₀ = x₀ · y₀
z₂ = x₁ · y₁
z₁ = (x₀ + x₁) · (y₀ + y₁) - z₀ - z₂

因为:

text
(x₀+x₁)(y₀+y₁)
  = x₀y₀ + x₀y₁ + x₁y₀ + x₁y₁

所以:

text
z₁ = x₀y₁ + x₁y₀

最终组合:

text
z = z₂ · r^n + z₁ · r^{n/2} + z₀

伪代码可以写成:

text
Karatsuba(x, y):
    if x or y is a single digit:
        return x · y

    split x into x₁ · r^{n/2} + x₀
    split y into y₁ · r^{n/2} + y₀

    z₀ ← Karatsuba(x₀, y₀)
    z₂ ← Karatsuba(x₁, y₁)
    z₁ ← Karatsuba(x₀ + x₁, y₀ + y₁) - z₀ - z₂

    return z₂ · r^n + z₁ · r^{n/2} + z₀

Recurrence

Karatsuba 只执行 3 次 half-sized multiplication,其他 addition、subtraction、shift 和组合总共是 Θ(n):

text
T(n) = 3T(n/2) + Θ(n)

因此:

text
T(n) = Θ(n^{log₂3})
     = Θ(n^{1.5849625...})

这比 Θ(n²) 更好。讲义用 branching factor 对比直观展示:

text
4 branches × depth log₂n → n^{log₂4} = n²
3 branches × depth log₂n → n^{log₂3} ≈ n^{1.585}

Python 的 big-integer implementation 会使用 Karatsuba 以及更先进的 multiplication techniques,具体策略取决于 operand size。

几何例子与 Catalan numbers

讲义随后给出一个有趣的 geometry problem。设一个很长的 chord AB 长度为:

text
AB = 1,000,000,000,000

中点为 C,从中点垂直移动到 D,满足:

text
BD = 1

要求计算 AD。由直角三角形关系:

text
AD = AC - CD
   = 500,000,000,000
     - √(500,000,000,000² - 1)

假设我们有 high-precision division,希望计算 AD 到一百万位。使用 Newton’s method 进行高精度平方根计算时,会在结果的每隔 24 个 decimal positions 中观察到 Catalan numbers。

这看起来非常意外:为什么一个几何长度的 decimal expansion 会包含 balanced-parentheses counting sequence?讲义给出一个不要求考试掌握的 power-series explanation。

Power series

令 real-valued function Q 的 power series 为:

text
Q(x) = c₀ + c₁x + c₂x² + c₃x³ + ...

考虑方程:

text
Q(x) = 1 + xQ(x)²

将 Q(x) 展开并平方:

text
1 + xQ(x)²
  = 1
    + c₀² x
    + (2c₀c₁) x²
    + (c₀c₂ + c₁² + c₂c₀) x³
    + ...

要让 Q(x)=1+xQ(x)² 成立,左右两侧每个 power 的 coefficients 必须相等:

text
c₀ = 1
c₁ = c₀²
c₂ = c₀c₁ + c₁c₀
c₃ = c₀c₂ + c₁c₁ + c₂c₀

这正是 Catalan recurrence,因此 Q(x) 的 coefficients 就是 Catalan numbers。

解二次方程

将 Q = 1 + xQ² 改写为关于 Q 的 quadratic equation,可以得到:

text
Q(x) = (1 ± √(1 - 4x)) / (2x)

选择具有正确 power-series expansion 的负平方根分支:

text
Q(x) = (1 - √(1 - 4x)) / (2x)

把 x = 10^{-24} 代入,并乘以 10^{-12},会得到:

text
500,000,000,000
  - √(500,000,000,000² - 1)

另一方面,从原始 power series 看:

text
10^{-12} Q(10^{-24})
  = c₀10^{-12} + c₁10^{-36} + c₂10^{-60}
    + c₃10^{-84} + ...

所以这个几何量的 decimal expansion 会在每隔 24 位出现一个 Catalan number。这就是讲义中 high-precision geometry example 与前面 Catalan digression 之间的联系。

复杂度总结

任务Recurrence / 方法复杂度
n-digit schoolbook multiplicationT(n)=4T(n/2)+Θ(n)Θ(n²)
Karatsuba multiplicationT(n)=3T(n/2)+Θ(n)Θ(n^{log₂3})≈Θ(n^{1.585})
Newton square-root iterationx_{i+1}=1/2(x_i+a/x_i)quadratic convergence
high-precision square rootNewton + high-precision division取决于 multiplication / division algorithm
Catalan number C_{n+1}∑_{k=0}^n C_kC_{n-k}递推计算需进一步优化

Karatsuba 的改进来自减少 recursive multiplications,而不是减少每一层的 additions。分治 algorithm 的关键问题通常是:哪些子问题真的需要递归求解,哪些 cross terms 可以通过代数变换共享。

小结

本讲从 irrational numbers 的 decimal expansion 出发,说明 high-precision arithmetic 的需求。Newton’s method 以 tangent approximation 迭代求 root,对 square root 展现 quadratic convergence,但它需要 high-precision division。

将两个 large integers 拆成 high / low halves 后,直接计算四个 partial products 得到 Θ(n²)。Karatsuba 利用:

text
(x₀+x₁)(y₀+y₁)-x₀y₀-x₁y₁ = x₀y₁+x₁y₀

把递归乘法从 4 次降为 3 次,得到 Θ(n^{log₂3})。最后的 geometry example 展示,high-precision computation 不仅提高数值精度,也可能暴露 power series、Catalan numbers 等隐藏的数学结构。

参考资料

← 返回列表
← 上一篇 Open Addressing 与 Cryptographic Hashing 下一篇 → Square Roots、Newton’s Method 与 High-Precision Division

评论 (0)

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