Guest User

Untitled

a guest
Jul 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. const PRIME_BASE = 257;
  2. const PRIME_MOD = 1000000007;
  3.  
  4. function polynomialHash(s) {
  5. let hash = 0;
  6. for (let i = 0; i < s.length; i++) {
  7. hash = hash * PRIME_BASE + s.charCodeAt(i);
  8. hash %= PRIME_MOD; //don't overflow
  9. }
  10.  
  11. return hash;
  12. }
  13.  
  14. console.log("polynomialHash(Paris)", polynomialHash("Paris"));
  15. console.log("polynomialHash(Auckland)", polynomialHash("Auckland"));
  16. console.log("polynomialHash(Peter)", polynomialHash("Peter"));
  17. console.log("polynomialHash(Dover)", polynomialHash("Dover"));
  18.  
  19. // Outputs
  20. // polynomialHash(Paris) 651721837
  21. // polynomialHash(Auckland) 56642560
  22. // polynomialHash(Peter) 719751278
  23. // polynomialHash(Dover) 539984858
Add Comment
Please, Sign In to add comment