Skip to content

Instantly share code, notes, and snippets.

@iamtrk

iamtrk/primes.js

Created Jul 23, 2013
Embed
What would you like to do?
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