Sub topic 2
Modular Arithmetic You Need for Hashing
This chapter assumes you can write basic Python and know what the % operator does with positive integers. It covers the few facts about modular arithmetic that make string hashing work. It also covers the errors that most often break it.
Why Hashes Need a Modulus
A string hash treats the characters as digits of a large number. A 20-character string with base 256 produces a number of about 160 bits. Python can store integers that large, but arithmetic on them gets slower as they grow. In Java or C++, a value that large overflows a 64-bit integer.
Arithmetic modulo m keeps every value in the range 0 to m - 1. The usual choice for m is a large prime such as 1_000_000_007. A later chapter explains why primes are preferred.
The Rules That Make It Work
Modular reduction commutes with addition, subtraction, and multiplication. That means you can reduce at any intermediate step without changing the final remainder:
| Operation | Identity |
|---|---|
| Addition | `(a + b) % m == ((a % m) + (b % m)) % m` |
| Subtraction | `(a - b) % m == ((a % m) - (b % m)) % m` |
| Multiplication | `(a * b) % m == ((a % m) * (b % m)) % m` |
Division is different. Dividing by x modulo m requires a modular inverse, so ordinary / does not work. The hashing methods in this tutorial use only the three operations in the table.
The following code checks this property directly. It builds the full number without reduction, then builds it again with reduction at each step:
base, mod = 256, 1_000_000_007
codes = [ord(c) for c in "hashing"]
big = 0
for c in codes:
big = big * base + c # grows without bound
small = 0
for c in codes:
small = (small * base + c) % mod # stays below mod
print(big.bit_length()) # 56
print(big % mod == small) # TruePowers Modulo m
The rolling update needs base^(m-1) mod m. Do not compute base ** k % mod, because that builds the huge power first. Python's three-argument pow reduces as it goes and finishes in O(log k) multiplications:
mod = 1_000_000_007
print(pow(256, 9, mod)) # fast, small intermediates
print(pow(256, 9, mod) == 256**9 % mod) # TrueCommon Errors
Negative Remainders
Subtraction can produce a negative number before reduction. Python's % always returns a result with the sign of the divisor, so (2 - 5) % 7 is 4. In Java, C, and C++, % keeps the sign of the dividend, so the same expression gives -3. The two hashes then disagree and real matches get missed.
Reducing Only at the End
Python still gives the correct answer if you reduce only once at the end, which hides the bug. The cost is speed: every operation runs on a growing big integer. Reduce after every multiply and add.
Overflow Before Reduction
With a modulus near 10^9, each reduced value fits in 30 bits, so the product of two of them fits in a signed 64-bit integer. A modulus near 2^63 would overflow during the multiplication itself. Pick the modulus so that (m - 1) * (m - 1) fits in the type you use.
Exercise
Compute (7 * 8 + 5) mod 11 by reducing after each step. Then state what (3 - 9) % 5 evaluates to in Python and in Java.
Show answer
7 * 8 = 56, and 56 % 11 = 1. Then 1 + 5 = 6, and 6 % 11 = 6. Reducing once at the end agrees, since 61 % 11 = 6. For 3 - 9 = -6, Python gives -6 % 5 = 4 and Java gives -1. Writing ((-6 % 5) + 5) % 5 in Java gives 4.
Exercise
Why is pow(base, k, mod) preferred over base ** k % mod for large k?
Show answer
The three-argument form reduces after each multiplication and uses repeated squaring, so it needs O(log k) small multiplications. base * k first builds a number with about k log2(base) bits, which costs time and memory.