TAOCP Vol 2: Seminumerical Algorithms · Week 78
TAOCP 4.3.1 The Classical Algorithms
Updated
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.
This week's steps
3 stepsRead §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.
Start with Ex 6, Ex 16, Ex 19; log what you solve below.
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.
Exercise 19M21▶ Recommended
The refinement that makes the trial quotient digit almost always correct.
Exercise 2224▶ Recommended
Find a case where the rare add-back step is needed. Use it as a test case.
Exercise 41M26▶ Recommended
Montgomery multiplication for repeated work modulo a fixed number. The basis of fast modular exponentiation.
Exercise 4322▶ Recommended
Exact rounded scaling of 8-bit color values without division. A real graphics trick.
Your log
Sign in to log the exercises you work: number, rating, how far you got.
Your notes
Only youSign 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 postsQuestions 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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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```spoilerblock.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.