Created
July 23, 2013 06:32
-
-
Save iamtrk/6060256 to your computer and use it in GitHub Desktop.
Prime numbers code written in Node.js
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
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