Hash function
Chaining and probing keep the table working when there are collisions, but they don't reduce them. What decides how many collisions there are is the hash algorithm. A good algorithm does 3 things: it's deterministic, meaning the same key always gives the same hash; it's fast to compute; and it spreads keys evenly across the buckets. If every key landed in 1 bucket, the table would be a list: O(n).
Hash algorithms are useful for much more than tables. For storing passwords: the system doesn't store the password but its hash, and when you log in it compares the hash of what you typed with the stored one. For checking that a file arrived intact: the file is sent with its hash, and the receiver computes the hash of what arrived; if they match, nothing changed along the way.
For security uses, like passwords, a hash needs 3 more properties. One-way: from the hash you can't figure out the input. Collision resistance: it must be nearly impossible to find 2 different inputs with the same hash. And the avalanche effect: a tiny change in the input changes the result hugely and unpredictably. Spreading evenly isn't enough: key % 100 spreads evenly, but anyone can find 2 keys that collide.
Many hash algorithms end by taking the remainder of dividing by a large number, and it's best for it to be prime. If the modulus has divisors and the keys follow a pattern, they pile up. With modulus 9, the keys 0, 3, 6, 9, 12... give only 0, 3 and 6: they use 3 buckets out of 9. With modulus 13, which is prime, the same keys give 0, 3, 6, 9, 12, 2, 5, 8...: they use them all. That's why a large prime is usually used, like 1,000,000,007.
