TAOCP Vol 2: Seminumerical Algorithms · Week 78

TAOCP 4.3.1 The Classical Algorithms

Updated

About 10 hours43 exercises, rated 10 to 42

Summary

TAOCP 4.3.1 The Classical Algorithms: Grade-school arithmetic on arbitrarily long integers: addition, subtraction, multiplication and especially long division, where Knuth gives a careful algorithm for estimating each quotient digit and proves it is almost always right. Budget about 10 hours; do exercises 6, 16 and 19.

Flashcards 14 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

This week's steps

3 steps
  1. Read §4.3.1 The Classical Algorithms

    Implement all four operations for numbers stored as arrays of 32-bit digits in C, with 64-bit intermediates.

  2. Work the exercises (8 picked)

    Start with Ex 6, Ex 16, Ex 19; log what you solve below.

  3. Write down one thing you'd explain to someone

    A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.

Sign in to track this: tick each step off and it counts toward your week.

Exercises

This section has 43 exercises, rated 10 to 42, and Knuth marks 11 of them with the triangle for recommended. Implement Algorithms A, S, M and D in a real language with a small radix so you can test against built-in big integers; most picks are about getting long division exactly right.

Worth your time

  • Exercise 622▶ Recommended

    Add left to right without ever revising a digit you have already written.

    Discuss

  • Exercise 1620▶ Recommended

    Short division by a single digit. You will use it constantly.

    Discuss

  • Exercise 19M21▶ Recommended

    The refinement that makes the trial quotient digit almost always correct.

    Discuss

  • Exercise 2224▶ Recommended

    Find a case where the rare add-back step is needed. Use it as a test case.

    Discuss

  • Exercise 28M30

    Svoboda's idea for making the divisor easy to divide by. The stretch.

    Discuss

  • Exercise 3720▶ Recommended

    Skip normalization when the scale factor is a power of two.

    Discuss

  • Exercise 41M26▶ Recommended

    Montgomery multiplication for repeated work modulo a fixed number. The basis of fast modular exponentiation.

    Discuss

  • Exercise 4322▶ Recommended

    Exact rounded scaling of 8-bit color values without division. A real graphics trick.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.

Discussion

0 posts

Questions about the reading go here; each exercise has its own thread below. Hints over answers.

No posts yet. Ask a question or share a tip.

Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Exercise threads

One thread per exercise. Open one to ask for a nudge or check your thinking.

  • Exercise 622No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 1620No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 19M21No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 2224No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 28M30No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 3720No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 41M26No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

  • Exercise 4322No posts yet

    Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler: ||like this|| or a ```spoiler block.

    Sign in to postAnyone can read. Posting needs an account with a confirmed email.

Reference

Why it matters

Every bignum library (Python ints, Java BigInteger, crypto libraries) is built on these. The long division algorithm is notoriously easy to get subtly wrong.

Before you start
  • 4.1 Positional Number Systems
How to approach it

Implement all four operations for numbers stored as arrays of 32-bit digits in C, with 64-bit intermediates. Test against Python's integers on random inputs. Spend most of the time on division and its normalization step.

Common traps
  • Forgetting to normalize the divisor before division.
  • Overflow in the quotient-digit estimate.
  • Edge cases: divisor with one digit, leading zero digits, dividend smaller than divisor.

Spot a mistake? Report it

Back to week 78