Exercise 2: Two Real Systems That Secretly Rely on Modular Arithmetic — Possible Solution ==================================================================== SYSTEM 1: HASH TABLE BUCKET INDEXING ------------------------------ Nearly every hash table implementation, in every mainstream language, computes which bucket a key belongs in with an expression like hash(key) % table_size. This is modular arithmetic directly - the hash function can produce an arbitrarily large integer, but the % operator maps that huge range down onto exactly table_size possible buckets (0 through table_size - 1), using the exact same "wraps around" behavior this chapter described for modular arithmetic generally. Without the modulus operation, a hash table would need a separate array slot for every possible hash value, which is completely impractical - the modulus is what makes a hash table's fixed-size backing array possible at all. SYSTEM 2: CREDIT CARD NUMBER VALIDATION (THE LUHN ALGORITHM) ------------------------------ Every payment form that instantly flags "that's not a valid card number" before ever contacting a payment processor is running the Luhn algorithm - a checksum computed entirely with modular arithmetic (specifically, checking whether a particular weighted sum of the card's digits is congruent to 0 modulo 10). This isn't cryptographic security; it's a fast, purely mathematical sanity check that catches typos and single-digit transcription errors before any real network request is made, saving a round trip to a payment processor for input that was never going to be valid in the first place. WHY THESE TWO ARE GENUINELY DIFFERENT ------------------------------ Hash table indexing uses modular arithmetic to solve a completely different problem (mapping an unbounded range of values down onto a small fixed set of buckets for fast lookup) than the Luhn algorithm does (detecting simple data-entry errors via a checksum before the data is even used). One is about efficient data structure design; the other is about cheap, local error detection. Both rely on the exact same underlying mathematical tool this chapter introduced, applied to two unrelated real problems. WHY THIS WORKS AS AN ANSWER ------------------------------ Both systems are named specifically (not vaguely gestured at), tied directly to this chapter's own stated connections table, and explicitly justified as genuinely different rather than two versions of the same underlying idea - one is a data-structure technique, the other is an error-detection technique, both built on modular arithmetic for unrelated reasons.