Problem Solving — CS & DS

Lecture 05 — Numbers, Remainders, and Cycles

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The pager schedule as a cycle

cs-017

On-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-018

Peel 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 · Beginner

The 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?

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-017 — discuss

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 · Foundational

Digits and Their Sum

Compute the digit sum of a positive integer, and decide divisibility by 3 from it. Uses // and % to peel digits.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-018 — discuss

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 · Intermediate

The 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 %.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-019 — discuss

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 · Advanced

The 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.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-020 — discuss

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

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. Compute 17 mod 5 and 17 // 5, and say what each means for a 5-day cycle.
  2. Why is remainder better than an if-chain for schedules?
  3. 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)
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF