Created
May 18, 2012 10:25
-
-
Save oodavid/2724526 to your computer and use it in GitHub Desktop.
Prime Numbers Algorythm
This file contains 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
/** | |
* You can check if a number is a prime by looping the known primes to see if it divides. | |
*/ | |
// Seed the primes array | |
var primes = [2,3]; | |
// Start counting up... | |
myCounter : for(n=5; n<10000; n+=2){ | |
// Loop the known primes | |
for(p=0, l=primes.length; p<l; p++){ | |
var prime = primes[p]; | |
// If it divides by a prime... | |
if((n % prime) == 0){ | |
// Not a prime | |
continue myCounter; | |
} | |
} | |
// Awesome | |
primes.push(n); | |
} | |
console.log(primes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment