TAOCP Vol 1: Fundamental Algorithms · Week 33
TAOCP 1.2.4 Integer Functions and Elementary Number Theory
Updated
Summary
TAOCP 1.2.4 Integer Functions and Elementary Number Theory: Floor, ceiling, the mod operation defined for any real numbers, and the basics of congruences: divisibility, primes, relatively prime numbers, and Fermat's theorem. Budget about 7 hours; do exercises 4, 6 and 8.
This week's steps
3 stepsRead §1.2.4 Integer Functions and Elementary Number Theory
Work every identity about floor and ceiling with a few positive and negative examples.
Start with Ex 4, Ex 6, Ex 8; 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 49 exercises, rated 00 to HM46, and Knuth marks 14 of them with the triangle for recommended. Lots of short M10 to M15 proofs here; do a handful to get fluent. The floor and mod identities pay off immediately in real code.
Worth your time
Exercise 4M10▶ Recommended
Relate the floor of -x to the ceiling of x. The identity behind half the off-by-one fixes you will ever write.
Exercise 620▶ Recommended
Decide which floor and ceiling of square root identities really hold.
Exercise 800
Quick mod values with negative operands. Compare each one with what % gives in your language.
Exercise 1010▶ Recommended
Mod with non-integer and negative arguments under Knuth's definition.
Exercise 19M10▶ Recommended
Prove modular inverses exist using extended Euclid. The basis of division mod m.
Exercise 35M20▶ Recommended
Show that nested integer divisions can be flattened. Why floor((x+m)/n) does not care whether x was floored first.
Exercise 37M30▶ Recommended
Evaluate a floor sum in closed form, with the gcd showing up. The stretch.
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 4M10No 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 620No 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 800No 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 1010No 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 1412No 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 19M10No 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 35M20No 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 37M30No 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
Floor and mod show up in every array index calculation, hash function, ring buffer and pagination bug you will ever write. Knuth's definition of mod for negative numbers is worth comparing with how your favorite language does it, because they differ.
Before you start
- 1.2.2 Numbers, Powers, and Logarithms
How to approach it
Work every identity about floor and ceiling with a few positive and negative examples. Then write a short program in C or Python comparing x mod y for negative x in Knuth's definition versus your language's % operator. Read the congruence material with pencil; it returns in Vol 2 chapter 4.
Common traps
- Assuming floor(-x) = -floor(x). It does not.
- Assuming C's % is Knuth's mod. For negative operands it is not.