Skip to content

Instantly share code, notes, and snippets.

@iamtrk
Created July 23, 2013 06:32
Show Gist options
  • Save iamtrk/6060256 to your computer and use it in GitHub Desktop.
Save iamtrk/6060256 to your computer and use it in GitHub Desktop.
Prime numbers code written in Node.js
var fs = require("fs");
function getPrimes(max) {
var sieve = [], i, j, primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
primes.push(parseInt(i));
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
}
return primes;
}
function sortNumber(a,b)
{
return a - b;
}
var numbs = getPrimes(54199);
numbs.sort(sortNumber);
var outFile = "prime.txt"
for(var i=0;i<numbs.length-1;i++){
fs.appendFileSync(outFile, numbs[i]+"," );}
fs.appendFileSync(outFile, numbs[numbs.length-1] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment