Created
August 20, 2012 10:22
-
-
Save josher19/3402903 to your computer and use it in GitHub Desktop.
hashing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// von n semi-random, but repeats too quickly | |
function nextr(n) { var d=(n*n); if (d%100==0) d /= 100; if (d < 100) d = d*5242870; d=d.toString(); var s=d.length; return d.substring(s-6, s-2)-0 } | |
function nextr(n) { var d=(n*n); if (d%100==0) d /= 100; if (d < 100) d *= 132645290; d=d.toString(); var s=d.length; return d.substring(d.length / 2 - 2, d.length / 2 + 2)-0 } | |
function testn(n,max) { max=max||1000; for(var h={}, i=0; i<max; ++i) { n = nextr(last=n); if(h[n]) break; h[n]=last||true; } return [i,n,last,h]; } | |
// primes | |
function isprime1(n) { if(n===2 || n===5) return true; if (n%2 == 0 || n%5 ==0) return false; var ps = [3,7,11,13,17,19,23,29], c=-1, limit=1+Math.round(Math.sqrt(n)/30); while(++c < limit) { for(var i in ps) {if (n % (last=ps[i] + c*30) === 0) return ps[i]} } return true} | |
function isprime(n) { if(n===2 || n===5) return true; if (n%2 == 0 || n%5 ==0) return false; var ps = [3,7,11,13,17,19,23,29], c=-1, limit=1+Math.round(Math.sqrt(Math.min(16331239353195370,n))); while(++c < limit) { for(var i in ps) {if (n % (last=ps[i] + c*30) === 0) return [false,last,n/last]} } return [true,limit]} | |
// hashing | |
function bern(key, len, level) { var hash = level || 0, len=len||key.length, i; for(i=0; i<len; ++i) hash = 33*hash + key[i]; return hash; } | |
function fromAsc(that) { return parseInt(that, 36) } | |
function toKey32(str) { return str.split("").map(fromAsc); } | |
function keyify(str) { return str.split(/[^A-Z0-9]/i); } | |
function toAsc(n) { return n.toString(36); } | |
// keyify("Joshua Shane Weinstein").map(toKey32)[2].map(toAsc).join("") | |
// keyify("Joshua Shane Weinstein 1234567890 [email protected]").map(fromAsc).map(toAsc).join(" ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment