# TAOCP 1.2.4 Integer Functions and Elementary Number Theory

> Study guide to TAOCP 1.2.4 Integer Functions and Elementary Number Theory. Floor, ceiling, the mod operation defined for any real numbers, and the basics of…

Updated October 20, 2026.

## Summary {#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.

## Key facts {#key-facts}

- Week 33 of 145 on the cracked.guide path, in [TAOCP Vol 1: Fundamental Algorithms](https://cracked.guide/taocp/volume-1).
- Time budget: about 7 hours.
- 49 exercises in the section, rated 00 to HM46; the guide picks 8.
- Picked exercises with Knuth's triangle for recommended: 4, 6, 10, 14, 19, 35 and 37.
- Before you start: 1.2.2 Numbers, Powers, and Logarithms.

## Week 33 goals {#objectives}

By the end of week 33 you can:

- Compute floors, ceilings and x mod y with negative arguments.
- Explain why Knuth's mod differs from C's % operator on negatives.
- Use congruences and Fermat's theorem on small examples.

## This week's steps {#steps}

- Read §1.2.4 Integer Functions and Elementary Number Theory: Work every identity about floor and ceiling with a few positive and negative examples.
- Work the exercises (8 picked): 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.

## Exercises {#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:

- 4 (rated M10, recommended): Relate the floor of -x to the ceiling of x. The identity behind half the off-by-one fixes you will ever write.
- 6 (rated 20, recommended): Decide which floor and ceiling of square root identities really hold.
- 8 (rated 00): Quick mod values with negative operands. Compare each one with what % gives in your language.
- 10 (rated 10, recommended): Mod with non-integer and negative arguments under Knuth's definition.
- 14 (rated 12, recommended): Combine two remainders into one. A tiny Chinese remainder problem.
- 19 (rated M10, recommended): Prove modular inverses exist using extended Euclid. The basis of division mod m.
- 35 (rated M20, recommended): Show that nested integer divisions can be flattened. Why floor((x+m)/n) does not care whether x was floored first.
- 37 (rated M30, recommended): Evaluate a floor sum in closed form, with the gcd showing up. The stretch.

## Why it matters {#why}

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 {#before}

- 1.2.2 Numbers, Powers, and Logarithms

## How to approach it {#approach}

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 {#traps}

- Assuming floor(-x) = -floor(x). It does not.
- Assuming C's % is Knuth's mod. For negative operands it is not.

## Links {#links}

- [Knuth's TAOCP page (editions, errata, MIX/MMIX)](https://www-cs-faculty.stanford.edu/~knuth/taocp.html)
- [Vol 1 errata (plain text, from Knuth)](https://www-cs-faculty.stanford.edu/~knuth/err1.textxt)
- [Wikipedia: Floor and ceiling functions](https://en.wikipedia.org/wiki/Floor_and_ceiling_functions)
- [Wikipedia: Modulo operation](https://en.wikipedia.org/wiki/Modulo)
- [Wikipedia: Fermat's little theorem](https://en.wikipedia.org/wiki/Fermat%27s_little_theorem)

---

Source: https://cracked.guide/guide/knuth-w020-0
cracked.guide: A free, dated 145-week path to getting genuinely cracked at programming.
