Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const PRIME_BASE = 257;
- const PRIME_MOD = 1000000007;
- function polynomialHash(s) {
- let hash = 0;
- for (let i = 0; i < s.length; i++) {
- hash = hash * PRIME_BASE + s.charCodeAt(i);
- hash %= PRIME_MOD; //don't overflow
- }
- return hash;
- }
- console.log("polynomialHash(Paris)", polynomialHash("Paris"));
- console.log("polynomialHash(Auckland)", polynomialHash("Auckland"));
- console.log("polynomialHash(Peter)", polynomialHash("Peter"));
- console.log("polynomialHash(Dover)", polynomialHash("Dover"));
- // Outputs
- // polynomialHash(Paris) 651721837
- // polynomialHash(Auckland) 56642560
- // polynomialHash(Peter) 719751278
- // polynomialHash(Dover) 539984858
Add Comment
Please, Sign In to add comment