Skip to content
TAOCP Companion

Closed Forms

An important theme of induction is determining closed forms from recursive functions and summations. This is a tool where you can use TEX notation to describe these and see how different values play out.

  • x2x^2

  • 2x−12x - 1

  • ∑k=1xg(k)\sum_{k=1}^{x} g(k)

  • {0x=02p(x−1)+1x>0\begin{cases} 0 & x = 0 \\ 2p(x-1) + 1 & x > 0 \end{cases}

Working out the values…

Extended Euclid's Algorithm

Knuth provides an extended form of Euclid's Algorithm to illustrate the concepts in this section. Here is a table to explore how different values behave going through that algorithm.

a′ab′bcdqr
100117695513116
011-3551116487
1-4-31311687129
-4513-16872930
  • 5⋅1769−16⋅551=295 \cdot 1769 - 16 \cdot 551 = 29
  • gcd⁡(1769,551)=29\gcd(1769, 551) = 29

Implementation

export function extended_euclids_algorithm(m: number, n: number) {
  // E1
  let a_prime = 1
  let b = 1
  let a = 0
  let b_prime = 0
  let c = m
  let d = n

  while (true) {
    // E2
    const q = Math.floor(c / d)
    const r = c % d

    if (r == 0) return { remainder: d, a, b } // E3

    // E4
    c = d
    d = r
    let t = a_prime
    a_prime = a
    a = t - q * a
    t = b_prime
    b_prime = b
    b = t - q * b
  }
}

Creating Diagrams

To help you explore Assertions within diagrams more extensively, here is a tool to assist you in creating them and labelling assertions. Connect to other steps by mentioning their label (e.g. E1, E2, etc.). To branch, state "If condition, whatever." The word "terminate" signals the end of the algorithm. If you hover over or click a step in the diagram, it will show what's needed to prove that step.

  1. A1 [Start ⇒ E1]
  2. E1.
  3. A2 [E1 ⇒ E2]
  4. E2.
  5. A3 [E2 ⇒ E3]
  6. E3.
  7. A4 [E3 ⇒ End]
  8. A5 [E3 ⇒ E4]
  9. E4.
  10. A6 [E4 ⇒ E2]

---
config:
  forceLegacyMathML: true
  flowchart:
    minNodeWidth: 0
---
flowchart LR
  subgraph algorithm["Algorithm E"]
    direction TB
    Start@{ shape: flip-tri, label: "Start" }
    E1["$$\begin{array}{c} \text{E1. }a \leftarrow 0\text{ }a' \leftarrow 1\text{ }c \leftarrow m \cr b \leftarrow 1\text{ }b' \leftarrow 0\text{ }d \leftarrow n \end{array}$$"]
    E2["$$\begin{array}{c} \text{E2. }q \leftarrow \text{quotient}(c / d) \cr r \leftarrow \text{remainder}(c / d) \end{array}$$"]
    E3["E3. If r = 0, terminate."]
    E4["$$\begin{array}{c} \text{E4. }c \leftarrow d\text{, }d \leftarrow r\text{;} \cr t \leftarrow a'\text{, }a' \leftarrow a\text{, }a \leftarrow t - qa\text{;} \cr t \leftarrow b'\text{, }b' \leftarrow b\text{, }b \leftarrow t - qb\text{.} \cr \text{ Go back to E2.} \end{array}$$"]
    End@{ shape: tri, label: "End" }
    Start --> E1
    E1 --> E2
    E2 --> E3
    E3 -- "$$(\text{r = 0})$$" --> End
    E3 -- "$$\lnot(\text{r = 0})$$" --> E4
    E4 --> E2
  end
  subgraph assertions["Assertions"]
    direction TB
    A1["$$\text{A1. }m \gt  0\text{, }n \gt  0\text{.}$$"]:::bare
    A2["$$\begin{array}{c} \text{A2. }c = m \gt  0\text{, }d = n \gt  0\text{,} \cr a = b' = 0\text{, }a' = b = 1\text{.} \end{array}$$"]:::bare
    A3["$$\begin{array}{c} \text{A3. }am + bn = d\text{, }a'm + b'n = c = qd + r\text{,} \cr 0 \leq r \lt  d\text{, }\text{gcd}(c, d) = \text{gcd}(m, n)\text{.} \end{array}$$"]:::bare
    A4["$$\text{A4. }am + bn = d = \text{gcd}(m, n)\text{.}$$"]:::bare
    A5["$$\begin{array}{c} \text{A5. }am + bn = d\text{, }a'm + b'n = c = qd + r\text{,} \cr 0 \lt  r \lt  d\text{, }\text{gcd}(c, d) = \text{gcd}(m, n)\text{.} \end{array}$$"]:::bare
    A6["$$\text{A6. }am + bn = d\text{, }a'm + b'n = c\text{, }d \gt  0\text{, }\text{gcd}(c, d) = \text{gcd}(m , n)$$"]:::bare
    A1 ~~~ A2 ~~~ A3 ~~~ A4 ~~~ A5 ~~~ A6
  end
  algorithm ~~~ assertions