Household Numbers: the Mathematics CapstoneHousehold Numbers
0 / 0

Workbench / Household Numbers: the Mathematics Capstone

S6 · B1·06

Shared Costs by Fractions

Stage 6, after chapter 6: rent split by room size, electricity by use and the internet equally, each as fractions of the whole that must add up to 1; exact shares in whole paise by the largest remainder rule; and a check that refuses shares like a half, a third and a quarter.

6 topics · ~6 min read
Updated Suggest a correction

Stage 6: shared costs by fractions

The brief

What you'll build. A splitter for shared household costs where people don't pay equally: rent in proportion to room size, electricity by an agreed ratio, the internet equally. Each rule is a set of fractions; the program checks they add up to the whole, works out every share exactly, and rounds to whole paise so the shares add up to the bill.

Why a real household needs this. Flatmates, joint families and couples share costs unequally all the time, and arguments come from two places: a rule nobody wrote down, and shares that don't add up ("I paid my half, you paid your third, who pays the rest?").

Why now. Chapter 6 gives the pieces: a share is a fraction of the whole, fractions from sizes are parttotal, fractions must be added with a common denominator, and exact arithmetic keeps every share honest until one final rounding. Stage 4 split a bill equally; this is the general case.

What you'll have at the end. fractions_split.py; your own household's sharing rules written as fractions; one month of bills split and checked.

Theory to own

  1. Shares from sizes. Rooms of 140, 110 and 110 square feet give shares 140360 =718 and 110360 =1136 each. Why do shares made this way always add up to 1? (Chapter: "Four readings of one number", the part reading.)
  2. A ratio as fractions. "2 : 1 : 1" means shares 24,14,14. Why do you divide by 2+1+1? (Chapter 8 does ratios properly.)
  3. The check. "Half, a third and a quarter" sounds fair. Add them with a common denominator: what's left over, or missing? (Chapter: "Different denominators: make the pieces the same".)
  4. One rounding, at the end. Why does rounding every share down and then handing out the leftover paise always give shares that add up exactly? (Exercises, senior exercise S3.)

Build it

Step 1: by hand. Write each shared cost and its rule. Turn each rule into fractions, add them to check they make 1, and work out one bill's shares to the paisa by hand. Note where a share isn't a whole number of paise.

Step 2: the code. All amounts in whole paise, shares as exact fractions:

Python
"""Stage 6: shared costs split by fractions of the whole, in whole paise that add up."""
from fractions import Fraction as F

def rupees(p):
    return f"₹{p // 100:,}.{p % 100:02d}"

def check_shares(shares):
    total = sum(shares.values())
    if total != 1:
        raise ValueError(f"shares add up to {total}, not 1")

def split(amount, shares):
    """Largest remainder: round every exact share down, then hand out the leftover paise."""
    check_shares(shares)
    exact = {k: amount * q for k, q in shares.items()}
    out = {k: q.numerator // q.denominator for k, q in exact.items()}
    left = amount - sum(out.values())
    for k in sorted(exact, key=lambda k: (-(exact[k] - out[k]), k))[:left]:
        out[k] += 1
    return out

# Rent by room size (square feet), electricity 2 : 1 : 1 (Asha works from home).
rooms = {"Asha": 140, "Bilal": 110, "Chitra": 110}
by_room = {k: F(v, sum(rooms.values())) for k, v in rooms.items()}
by_use = {"Asha": F(2, 4), "Bilal": F(1, 4), "Chitra": F(1, 4)}
bills = [("rent", 24_000_00, by_room), ("electricity", 1_837_00, by_use), ("internet", 999_00, None)]

owed = {k: 0 for k in rooms}
for name, amount, shares in bills:
    shares = shares or {k: F(1, len(rooms)) for k in rooms}
    parts = split(amount, shares)
    print(f"{name:<12} {rupees(amount):>11}  shares " + ", ".join(f"{k} {q}" for k, q in shares.items()))
    print(" " * 26 + ", ".join(f"{k} {rupees(p)}" for k, p in parts.items()), "| sum", rupees(sum(parts.values())))
    for k, p in parts.items():
        owed[k] += p
print("each pays:", ", ".join(f"{k} {rupees(p)}" for k, p in owed.items()), "| total", rupees(sum(owed.values())))

try:
    split(1_200_00, {"Asha": F(1, 2), "Bilal": F(1, 3), "Chitra": F(1, 4)})
except ValueError as e:
    print("refused:", e)
Text
rent          ₹24,000.00  shares Asha 7/18, Bilal 11/36, Chitra 11/36
                          Asha ₹9,333.34, Bilal ₹7,333.33, Chitra ₹7,333.33 | sum ₹24,000.00
electricity    ₹1,837.00  shares Asha 1/2, Bilal 1/4, Chitra 1/4
                          Asha ₹918.50, Bilal ₹459.25, Chitra ₹459.25 | sum ₹1,837.00
internet         ₹999.00  shares Asha 1/3, Bilal 1/3, Chitra 1/3
                          Asha ₹333.00, Bilal ₹333.00, Chitra ₹333.00 | sum ₹999.00
each pays: Asha ₹10,584.84, Bilal ₹8,125.58, Chitra ₹8,125.58 | total ₹26,836.00
refused: shares add up to 13/12, not 1

Step 3: read the output. Rent by room size gives Asha 718 of ₹24,000, which is ₹9,333.33 and a third of a paisa, and the others 1136 each, ₹7,333.33 and a third. Rounding all three down loses a paisa; the largest remainder rule gives it to Asha (all three fractional parts are equal, so the fixed tie-break, alphabetical order, decides), and the shares add up to ₹24,000.00 exactly. Electricity at 2 : 1 : 1 divides exactly. The internet's ₹999 splits equally into ₹333. The last line is the check at work: shares of 12,13,14 add up to 1312, more than the whole bill, so the program refuses them instead of collecting ₹100 too much.

Step 4: your rules. Run it with your own costs and rules. Then change the tie-break so the extra paisa rotates month by month instead of always going to the same person, and check over twelve months that nobody pays more than a few paise more than their exact share in total.

Senior and architect lens

  • Rules are data. Store each cost's sharing rule (sizes, a ratio, or equal) and derive the fractions; don't store pre-computed rupee amounts that go stale when the rent changes.
  • Validate the whole, not just the parts. Shares must add up to exactly 1, checked with exact fractions, not floats (13+13+13 as floats happens to make 1, but 0.1×3 doesn't make 0.3; the chapter's lab).
  • Exact until the last step, then one documented rounding. The same pattern as stage 4's equal split, now with any weights: compute exactly, round down, distribute the remainder by a stated rule.

Evidence

  • fractions_split.py with your own bills and rules, and its output.
  • Your hand calculation of one bill's shares, with the fractions adding to 1.
  • The twelve-month check from Step 4: how far each person's total is from their exact share.

Carry forward

Stage 7, after chapter 7, turns these fractions into percentages and decimals people recognise: each person's share as a percentage, a price rise or discount as a percentage change, and GST added to a bill, rounded the way an invoice does.