Skip to content

Instantly share code, notes, and snippets.

@ajace
Last active October 5, 2020 03:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajace/5882370 to your computer and use it in GitHub Desktop.
Save ajace/5882370 to your computer and use it in GitHub Desktop.
nodejs script to generate prime numbers
#!/usr/bin/env node
var fs = require('fs');
var outfile = "primes.txt";
var count = 0;
var maxCount = 100;
var primes = [];
var i = 2;
while(count<maxCount) {
if( isPrime(i) ) {
primes.push(i);
count++;
}
i++;
}
function isPrime (n)
{
if ( n%1 || n<2 ) return false;
var q = Math.sqrt(n);
for (var i = 2; i <= q; i++)
{
if (n % i === 0)
{
return false;
}
}
return true;
}
var result = primes.toString();
var out = result;
fs.writeFileSync(outfile, out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment