Mid-Square hashing is a hashing technique in which unique keys are generated. In this technique, a seed value is taken and it is squared. Then, some digits from the middle are extracted. These extracted digits form a number which is taken as the new seed. This technique can generate keys with high randomness if a big enough seed value is taken. However, it has a limitation. As the seed is squared, if a 6-digit number is taken, then the square will have 12-digits. This exceeds the range of int data type. So, overflow must be taken care of. In case of overflow, use long long int data type or use string as multiplication if overflow still occurs. The chances of a collision in mid-square hashing are low, not obsolete. So, in the chances, if a collision occurs, it is handled using some hash map.
Suppose a 4-digit seed is taken. seed = 4765
Hence, square of seed is = 4765 * 4765 = 22705225
Now, from this 8-digit number, any four digits are extracted (Say, the middle four).
So, the new seed value becomes seed = 7052
Now, square of this new seed is = 7052 * 7052 = 49730704
Again, the same set of 4-digits is extracted.
So, the new seed value becomes seed = 7307
This process is repeated as many times as a key is required.
Mid square technique takes a certain number of digits from the square of a number. This number extracted is a pseudo-random number, which can be used as a key for hashing.
- Choose a seed value. This is an important step as for same seed value, the same sequence of random numbers is generated.
- Take the square of the seed value and update seed by a certain number of digits that occur in the square. Note: The larger is the number of digits, larger will be the randomness.
- Return the key.
Below is the implementation of above algorithm:
Note: The output will change according to the date and time.
- Hashing in Distributed Systems
- Find the number of p-sided squares in a grid with K blacks painted
- Count number of pairs in array having sum divisible by K | SET 2
- Maximum length sub-array which satisfies the given conditions
- Find minimum difference between any two elements | Set 2
- Count number of distinct substrings of a given length
- Queries to check if string B exists as substring in string A
- Rearrange given string to maximize the occurrence of string t
- Sum of all odd frequency nodes of the Linked List
- Count pairs with average present in the same array
- Minimum cost to modify a string
- Number of times the given string occurs in the array in the range [l, r]
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Check if a given number divides the sum of the factorials of its digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.