Workbench / Household Numbers: the Mathematics Capstone
S5 · B1·05
Recurring Payments and When They Collide
Stage 5, after chapter 5: a twelve-month calendar of payments that come every 2, 3 or 6 months; when the pattern repeats (the lcm); which payments can ever land in the same month (the gcd and the offsets); and the steady monthly amount that smooths out the lumpy months.
Stage 5: recurring payments and when they collide
The brief
What you'll build. A planner for payments that don't come every month: school fees every quarter, an insurance premium every six months, a streaming plan billed every two. It lists what's due in each month, says when the whole pattern repeats, works out which pairs of payments can ever fall in the same month, and suggests one steady amount to set aside each month so the big months aren't a shock.
Why a real household needs this. Monthly bills are easy to plan for. The ones that come every few months are the ones that catch people out, especially when two land together: a month with the school fees and the insurance premium at once can need several times a normal month's spare money.
Why now. Chapter 5 gives exactly the tools: multiples ("due every 3 months" means the months form a sequence of multiples, shifted), the least common multiple (when two cycles line up again) and the greatest common divisor (whether they can line up at all).
What you'll have at the end. schedule.py; your own calendar of non-monthly payments for a year; the pairs that collide and the months they do; your monthly set-aside amount.
Theory to own
- Due months as remainders. A payment every months, first due in month , is due in month exactly when is a multiple of , i.e. . Why is that the same as " leaves the same remainder as on division by "? (Chapter: "Arithmetic with remainders".)
- When the pattern repeats. Why does the whole calendar repeat after of all the periods, and not sooner? (Chapter: "Greatest common divisor and least common multiple"; the worked lighthouse example.)
- When two payments can collide. Two payments every and months, first due in months and , are ever due in the same month exactly when is a multiple of . Try it: every 3 months from January and every 6 from February. (The Chinese remainder theorem, in the exercises' "What the chapter leaves out".)
- Smoothing. A year's total divided by 12, rounded up to the paisa, is enough to set aside each month. Why up, and why does the set-aside pot still dip in some months if you start it in the wrong month? (Chapter 4: rounding up with integers.)
Build it
Step 1: by hand. List every payment you make that isn't monthly: how often, which month it's first due, and how much. Draw twelve boxes, one per month, and write in each one what's due. Circle any month with two or more.
Step 2: the code. All amounts in whole paise, as in every stage:
"""Stage 5: recurring payments, when they land together, and the busiest months."""
from math import gcd
def lcm(a, b):
return a * b // gcd(a, b)
# name, every how many months, first month due (1 = January), amount in paise
payments = [
("School fees", 3, 1, 18_500_00),
("Insurance", 6, 2, 12_400_00),
("Streaming", 2, 1, 649_00),
]
def due(every, first, month):
return month >= first and (month - first) % every == 0
def rupees(p):
return f"₹{p // 100:,}.{p % 100:02d}"
print("month due total")
for m in range(1, 13):
names = [n for n, e, f, _ in payments if due(e, f, m)]
total = sum(a for n, e, f, a in payments if due(e, f, m))
print(f"{m:>5} {', '.join(names) or '-':<36} {rupees(total):>10}")
cycle = 1
for _, e, _, _ in payments:
cycle = lcm(cycle, e)
print("pattern repeats every", cycle, "months")
# Do two payments ever land in the same month? Only if their offsets
# differ by a multiple of gcd(every1, every2).
for i in range(len(payments)):
for j in range(i + 1, len(payments)):
n1, e1, f1, _ = payments[i]
n2, e2, f2, _ = payments[j]
g = gcd(e1, e2)
meet = (f1 - f2) % g == 0
verdict = f"together every {lcm(e1, e2)} months" if meet else "never together"
print(f"{n1} + {n2}: gcd {g}, offsets differ by {abs(f1 - f2)} -> {verdict}")
yearly = sum(a * (12 // e) for _, e, _, a in payments)
print("a year of these:", rupees(yearly), "| set aside each month:",
rupees(-(-yearly // 12)))
month due total
1 School fees, Streaming ₹19,149.00
2 Insurance ₹12,400.00
3 Streaming ₹649.00
4 School fees ₹18,500.00
5 Streaming ₹649.00
6 - ₹0.00
7 School fees, Streaming ₹19,149.00
8 Insurance ₹12,400.00
9 Streaming ₹649.00
10 School fees ₹18,500.00
11 Streaming ₹649.00
12 - ₹0.00
pattern repeats every 6 months
School fees + Insurance: gcd 3, offsets differ by 1 -> never together
School fees + Streaming: gcd 1, offsets differ by 0 -> together every 6 months
Insurance + Streaming: gcd 2, offsets differ by 1 -> never together
a year of these: ₹102,694.00 | set aside each month: ₹8,557.84
Step 3: read the output. The pattern repeats every 6 months: . School fees (every 3 from January) and insurance (every 6 from February) never coincide, because their first months differ by 1 and doesn't divide 1; the same goes for insurance and streaming (). School fees and streaming start together, so they meet every months, in January and July, the two heaviest months at ₹19,149. June and December have nothing due at all. A year costs ₹102,694, so ₹8,557.84 a month set aside (rounded up) covers it, but only if the pot is started early enough: the code checks the total, not the timing, which is the next step.
Step 4: your numbers, and the timing. Run it with your own payments. Then add a running balance: start the pot at zero in January, add the set-aside amount each month, and take each month's payments out. With the sample data the pot goes negative in January (₹19,149 due, ₹8,557.84 in). Find the smallest starting balance that keeps it at zero or above all year, and compare it with moving the first due date of one payment (many insurers and schools let you choose). Shifting the school fees to start in February would make them collide with the insurance instead; check with the gcd rule before you ask.
Senior and architect lens
- Schedules are remainders, not lists. Store "every 3 months from January" as a period and an offset, and compute due dates; a stored list of dates goes stale the first time a plan changes, and can't answer "when do these two next coincide?".
- Collisions are predictable. The gcd rule answers "can these two ever coincide?" without simulating years of months, and the lcm says how far ahead to look. The same arithmetic schedules cron jobs, batch runs and maintenance windows (exercises S3).
- Calendar months aren't uniform. This stage counts months as numbers 1 to 12. Real due dates on the 31st, or on "the last working day", need a rule for short months and holidays; state it, like the rounding rules in stage 4.
Evidence
schedule.pywith your own payments, and its output.- Your hand-drawn twelve-month calendar with collisions circled.
- The starting balance your running-balance check found, or the due date you would move instead.
Carry forward
Stage 6, after chapter 6, splits shared costs by fractions of the whole (rent shared 2 : 1 : 1, or by room size) so that every share is exact and the shares add up to the bill.