Skip to content

Instantly share code, notes, and snippets.

@oodavid
Created May 18, 2012 10:25
Show Gist options
  • Save oodavid/2724526 to your computer and use it in GitHub Desktop.
Save oodavid/2724526 to your computer and use it in GitHub Desktop.
Prime Numbers Algorythm
/**
* 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