Skip to the workspace
Pebellslearn by building
Number Theory for Algorithms  ›  Lesson

Modular Arithmetic That Doesn't Overflow

Modular Arithmetic in Code 22 minMod after every add or multiply, and normalize negatives yourself when porting between languages

dsa / Modular Arithmetic That Doesn't Overflow

Explore each scene at your own pace.

01 · Spreading load across shards

Spreading load across shards

Modular Arithmetic That Doesn't Overflow

Definition
Modular arithmetic works with remainders: a % m is what is left after dividing a by m, so every result stays inside a fixed range from 0 to m-1. Reducing after every addition or multiplication keeps running totals small.
In plain words
It is clock arithmetic. Five hours after 22:00 is 3:00, not 27:00, because the numbers wrap round to the start. Take the remainder after every step and the number never grows out of hand.

In short: a % m is the remainder after dividing a by m, and it always lands in [0, m) in Python — negatives wrap around to the top of the range instead of going below zero. Other languages (C, Java, JavaScript) truncate toward zero instead, so a negative a gives a negative remainder there. For a running total, mod after every add or multiply, not once at the end — the in-between value can overflow in other languages, and even in Python it just gets slower to work with as it grows into a giant integer.

A clock face with a token starting at a number and walking forward or backward around the rim, wrapping past 12 back to 1

Spreading load across shards

Quarry assigns a record to one of shard_count shards with record_id % shard_count. Record ids are usually positive, but a signed hash or a delta between two ids can go negative — and that's where languages disagree.

python

In Python this is always a safe shard index. Port the same formula to C, Java or JavaScript and -3 % 8 comes back -3 — a negative array index, which crashes or silently reads the wrong shard. Rule: if a value can go negative, normalize it yourself: ((x % m) + m) % m works the same in every language, because it adds one more m before the final mod, and a value already in range is unaffected.

What does this print?
def safe_mod(x, m):
    return ((x % m) + m) % m

print(safe_mod(-3, 8), safe_mod(11, 8), safe_mod(-16, 8))
Scene 1 of 8
Project context and further resources
You're building a piece ofQuarry — the hash kernel
This piece — poly_hash(): Folds a string into one number, modding every step so it never overflows or grows huge.
Scenario Quarry's hash kernel folds every key into a fixed-size number before it ever touches a bucket array, and it has to do that without the running total ballooning into a giant integer on every insert.
poly_hash.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
poly_hash(s, base, mod) → intFold s into one number mod `mod`, modding after every character.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.