Problem Solving — CS & DS
Lecture 05 — Numbers, Remainders, and Cycles
Learning objectives
- Use integer division and remainder to model cycles
- Reason about divisibility and digit sums
- Recognise when modulo simplifies a problem
Case-based lecture: you attempt, we discuss, then we reveal.
Today
The plan
- Four cases:
cs-017, cs-018, cs-019, cs-020 - Each: 5-minute attempt → discussion → reveal → prompt
- Worked examples in between — ask anything
- Break mid-lecture · exit question at the end
Key definitions
Key definitions
- Integer division `//` — division keeping only the whole part: `17 // 5` is 3.
- Remainder (modulo) `%` — what is left after whole divisions: `17 % 5` is 2.
- Cycle — a pattern repeating every k steps; `n % k` names the position inside the cycle.
- Digit extraction — `n % 10` is the last digit of n; `n // 10` removes it.
- Divisor — d divides n exactly when `n % d == 0`.
- Simulation — stepping a process rule by rule, as it would happen, instead of computing a formula.
Explain each in one breath; the cases will do the heavy lifting.
Worked example
The pager schedule as a cycle
cs-017On-call duty rotates among 5 people daily; person 1 is on call on day 1. Who is on call on day 47? And what changes if rotation skips weekends (day 1 = Monday)?
Expected: 2 then 5
Cost: O(1) for both variants - arithmetic beats simulating day by day.
Worked example
Digits, sums, and the divisible-by-3 rule
cs-018Peel the digits of 4723 by arithmetic and sum them. Is 4723 divisible by 3?
n = 4723
total = 0
while n > 0:
total += n % 10
n //= 10
print(total)
Expected: 16 then False
Cost: O(d) where d is the digit count (about log10 of n) - logarithmic in the value, which surprises students.
Case attempt · lecture 05
Case cs-017 · BeginnerThe Pager Schedule
On-call duty rotates among 5 people every day. Given the day number of the year and who starts, decide who is on call. Then: what changes if rotation skips weekends?
**Input:** 5 people, rotation starts with person 1 on day 1; day 47**Output:** day 47 -> ((47-1) mod 5)+1 = person 2; with weekend skip: count only weekdaysSolve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.
⏱ 5 minutes — attempt nowPseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Discussion
cs-017 — discuss
- What did the deciding quantity turn out to be?
- Which assumption shaped your answer?
- After the solution is revealed, the class spends five minutes on this
prompt: **"Propose one extra rule that would change the verdict — is it an improvement?"**
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-017 — not embedded in this deck.
Case attempt · lecture 05
Case cs-018 · FoundationalDigits and Their Sum
Compute the digit sum of a positive integer, and decide divisibility by 3 from it. Uses // and % to peel digits.
**Input:** 4723**Output:** digit sum 16 -> not divisible by 3; peel: 4723 % 10 = 3, then 472, ...Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.
⏱ 5 minutes — attempt nowPseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Discussion
cs-018 — discuss
- What did the deciding quantity turn out to be?
- Which assumption shaped your answer?
- After the solution is revealed, the class spends five minutes on this
prompt: **"Which assumption would you attack first if you had to argue the other side?"**
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-018 — not embedded in this deck.
Case attempt · lecture 05
Case cs-019 · IntermediateThe Broken Keypad
A keypad digit is dead; given a target number, output all numbers up to N that avoid that digit. Careful digit checking with // and %.
**Input:** dead digit 4, N = 30**Output:** 1,2,3,5,6,7,8,9,10,11,12,13,15,... (23 numbers below 30 avoid the digit 4)Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.
⏱ 5 minutes — attempt nowPseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Discussion
cs-019 — discuss
- What did the deciding quantity turn out to be?
- Which assumption shaped your answer?
- After the solution is revealed, the class spends five minutes on this
prompt: **"Propose one extra rule that would change the verdict — is it an improvement?"**
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-019 — not embedded in this deck.
Case attempt · lecture 05
Case cs-020 · AdvancedThe Josephus Ring
People stand in a circle; every k-th is removed until one remains. Derive the survivor with a recurrence, verify by simulation. A first taste of proof plus experiment.
**Input:** n = 7 people, k = 3**Output:** survivor position 4 (0-indexed 3)Solve for the worked instance first. Then ask what would change if onenumber, name, or condition were replaced by something stranger.
⏱ 5 minutes — attempt nowPseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.
Discussion
cs-020 — discuss
- What did the deciding quantity turn out to be?
- Which assumption shaped your answer?
- After the solution is revealed, the class spends five minutes on this
prompt: **"Which assumption would you attack first if you had to argue the other side?"**
Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-020 — not embedded in this deck.
Check yourself
Common misconceptions
- Treating % as trivia to memorise instead of a cycle-position tool.
- Starting cycles at 1 vs 0 - state the convention before computing.
- Extracting digits with strings when the arithmetic is the point of the exercise.
- Simulating the Josephus ring for large n instead of tabulating small cases and finding the pattern.
Summary
Summary & exit question
- Named technique today: Numbers, Remainders, and Cycles
- Four cases attempted — methods before answers
Exit question (choose one, 60 seconds)
- Compute 17 mod 5 and 17 // 5, and say what each means for a 5-day cycle.
- Why is remainder better than an if-chain for schedules?
- What replaced a loop in the Josephus analysis?
Expected answers (instructor): 2 and 3: day-before-cycle position and completed cycles one expression handles all positions; no special cases to maintain a table of small cases plus a conjecture (mathematics replacing simulation)