9.1Hash Functions
Multiplication, folding and mid-square
The division method is enough for most work, but it is not the only way. Textbooks and exams ask about three more methods: multiplication, folding and mid-square. All three serve the same goal: let every digit of the key count and fill the cells evenly.
- How the multiplication method picks a cell with the fractional part
- That folding splits the key into pieces and adds them
- Why mid-square uses the digits in the middle of the square
1Idea
The multiplication method
The key is multiplied by a constant A between 0 and 1, only the fractional part of the result is kept, and that fraction is multiplied by M and rounded down: h(k) = ⌊M · (k·A mod 1)⌋. The fraction is between 0 and 1, so the result is between 0 and M - 1. The constant A = (√5 - 1) / 2 ≈ 0.618 suggested by Knuth spreads keys well.
The plus of this method is that it does not care how M is chosen: the table size can even be a power of 2. The worry about primes in the division method does not apply here.
Folding
The key is split into pieces of equal length, the pieces are added, and the remainder of the sum divided by M is taken. 123456 split into two-digit pieces gives 12, 34 and 56; the sum is 102, and 102 % 11 = 3. For long keys such as phone or ID numbers, every digit contributes to the result.
Mid-square
The key is squared and the digits in the middle are used. The square of 1234 is 1522756; the middle two digits are 22, and 22 % 11 = 0. The middle of the square depends on every digit of the key, while the digits at the ends depend on only a few. The code drops three digits from the right and takes the next two; for four-digit keys this falls in the middle of the square.
| Method | Formula | For 1234 | When |
|---|---|---|---|
| Division | k % M | 2 | The default; M must be prime |
| Multiplication | ⌊M · (k·A mod 1)⌋ | 7 | When M must be free to choose |
| Folding | sum of pieces % M | (12 + 34) % 11 = 2 | Long numbers |
| Mid-square | middle of the square % M | 0 | When every digit matters |
2Step by step
The three methods are applied in turn: 1234 with multiplication, 123456 with folding, 1234 with mid-square. The table has 11 cells; the cell chosen in the end lights up green.
- Multiplication:
x = 1234 · 0.618... = 762.654. - The fractional part:
762.654 - 762 = 0.654. The whole part is dropped. 11 · 0.654 = 7.19, rounded down: cell 7.- Folding: 123456 is split into two-digit pieces from the right: 56, 34, 12.
- The sum is 102, and
102 % 11 = 3: cell 3. - Mid-square:
1234² = 1522756. Three digits are dropped from the right (1522) and the last two are taken: 22. 22 % 11 = 0: cell 0.
#define M 11 int multiplicationHash(int key) { double A = 0.6180339887; // (√5 - 1) / 2 double x = key * A; double frac = x - (long long)x; // fractional part return (int)(M * frac);} int foldingHash(int key) { int total = 0; while (key > 0) { total += key % 100; // two digits from the right key /= 100; } return total % M;} int midSquareHash(int key) { long long square = (long long)key * key; int mid = (int)(square / 1000 % 100); // the middle two digits return mid % M;}static final int M = 11; int multiplicationHash(int key) { double A = 0.6180339887; // (√5 - 1) / 2 double x = key * A; double frac = x - (long) x; // fractional part return (int) (M * frac);} int foldingHash(int key) { int total = 0; while (key > 0) { total += key % 100; // two digits from the right key /= 100; } return total % M;} int midSquareHash(int key) { long square = (long) key * key; int mid = (int) (square / 1000 % 100); // the middle two digits return mid % M;}M = 11 def multiplication_hash(key): A = 0.6180339887 # (√5 - 1) / 2 x = key * A frac = x - int(x) # fractional part return int(M * frac) def folding_hash(key): total = 0 while key > 0: total += key % 100 # two digits from the right key //= 100 return total % M def mid_square_hash(key): square = key * key mid = square // 1000 % 100 # the middle two digits return mid % MThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Multiplication and mid-square are a few arithmetic operations. Folding loops once per pair of digits; keys have a fixed length, so this is constant time too.
A few variables.
Whichever method is chosen, collisions happen. The method only decides whether they are frequent or rare.
4Common mistake
A common mistake in the multiplication method is forgetting to take the fractional part:
return (int)(key * A) % M; // wrong: the whole part was usedThis code computes 762 % 11; the method turns back into the division method and the spreading effect of A is lost. The idea of the multiplication method is that the fractional part depends on every bit of the key. In mid-square, long long in C and long in Java keep the square from overflowing.
5Try it
With M = 11, which cell does folding put the key 250013 in?
The answer is A. Two digits at a time from the right: 13, 00 and 25. The sum is 38, and 38 % 11 = 5.
Show the answer
The answer is A. Two digits at a time from the right: 13, 00 and 25. The sum is 38, and 38 % 11 = 5.
What is the plus of the multiplication method over the division method?
The answer is B. The fractional part decides the result; M only scales the fraction. So M can even be a number that suits the division method badly, such as a power of 2. Collisions still happen.
Show the answer
The answer is B. The fractional part decides the result; M only scales the fraction. So M can even be a number that suits the division method badly, such as a power of 2. Collisions still happen.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.