Mathematics I / Divisibility and the Division Algorithm
Practice question · Multiple choice

A hash function reduces a key modulo the table size. Why do implementations favour a prime table size?

Hints
  1. Take keys that are all multiples of 10 and a table of size 100. Which buckets get used?
  2. Ask what a shared factor between key pattern and table size does to the remainders.
Show the answer

A. Because a composite size shares factors with patterned keys

Why

Keys are rarely random, they are IDs, addresses, timestamps, often sharing a factor. With size 100 every multiple of 10 lands in ten buckets and ninety sit empty; a prime shares no factor with the pattern, so the remainders spread. Number theory earning its keep in a data structure.

Read the lesson: Divisibility and the Division Algorithm →

Practise Divisibility and the Division Algorithm

The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.

More questions on Divisibility and the Division Algorithm