Why table size can change a hash pattern

A hash table turns a key—such as a name or number—into a slot number. One common method takes a hash value and keeps the remainder after dividing by the number of slots. That remainder is the slot index.

The choice of divisor matters when the hash values have a pattern. Suppose they increase by a fixed step each time. If the table size shares a factor with that step, the remainders may cycle through only some of the slots. With 12 slots and a step of 4, for example, the sequence visits just three positions: 0, 4, and 8. Keys sent to those positions compete for the same small part of the table.

How a prime can help

A prime table size has no divisors other than 1 and itself. For a step smaller than that prime, the step and table size therefore share no factor. Repeatedly adding the step and taking the remainder will eventually visit every slot before the sequence repeats.

That can break up regular patterns in the hash values, especially when the method for turning a hash into a slot uses a remainder. It does not make keys collision-free: different keys can still land in the same slot, and uneven hash values can still produce clusters.

The practical lesson is that table size and hashing method work together. A prime can prevent certain patterns from getting trapped in a subset of slots—but the improvement comes from how the arithmetic interacts, not from primality acting like a universal fix.