Lecture Notes — Lecture 05: Numbers, Remainders, and Cycles

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

Remainder is a design tool. Whenever life repeats in cycles - weekdays, shifts, seats, keyboards - n % k names the position in the cycle and n // k counts completed laps. One operator replaces a wall of special cases.

Digits without strings. n % 10 is the last digit; n // 10 chops it off. Repeat to visit every digit. Doing this with arithmetic (not str(n)) builds the exact muscle lecture 20's number-theory scans need.

Reconstructing the input. cs-019 runs the machine backwards: given outputs, rebuild inputs. Reversible thinking doubles your test design power - you can generate cases where you know the answer.

Simulation vs formula. Some processes (Josephus ring) yield to simulation at small scale and to a table of values at larger scale; the pattern in the table replaces the loop. Knowing when mathematics replaces iteration is the advanced move.

Honesty about mod zero. n % k with k = 0 is an error, and n % 1 is always 0 - quick sanity checks when reading other people's cycle code.

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. n % k positions you in a cycle; n // k counts completed laps.
  2. n % 10 and n // 10 visit digits without strings.
  3. Tabulate small cases; let patterns replace loops.
  4. Reversing a process (outputs to inputs) doubles your testing power.

Practice questions

Where this leads

Next lecture: Traces and Loop Reasoning. The quiz below checks this lecture's essentials before we build on them.