I love prime numbers, and they have some amazing applications in cryptography as well as some other things.
This is a pretty useless application of primes, but it's a good demonstration I wrote to show off one of the amazing properties of prime numbers.
Given a word, work out if one word is a scrambled version of another. IE, boat and btoa is true, but bota and booat is false. Same number of characters, same characters.
Quote:
a=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61, 67,71,73,79,83,89,97,101];
function b(c)
{
r=1;
for(i=0;i<c.length;i++)
r*=a[c[i].charCodeAt(0)-97];
return r
}
Then do:
Quote:
alert(b("hello")==b("elloh"));
(It's obfuscated a bit as it was for a competition, it's the least obfuscated one I've got)
Anyone else got any interesting prime number snippets or applications? I'd love to see them!