Skip to main content

Polynomial Hash

Definition​

The Polynomial Hash Algorithm is a method used in computer science for hashing strings into unique numerical values. It involves treating the characters of a string as coefficients of a polynomial, evaluating this polynomial at a specific point, and taking the result modulo a prime number to avoid overflow and ensure uniform distribution of hash values

Practice​

polynomial_hash(input_string):
// Initialize variables
hash = 0
P = large_prime_number
B = base_value

// Iterate through each character in the input string
for each character c in input_string:
// Update hash using polynomial evaluation
hash = (hash * B + c) % P

// Return final hash value
return hash