Last active
October 5, 2020 03:03
-
-
Save ajace/5882370 to your computer and use it in GitHub Desktop.
nodejs script to generate prime numbers
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
#!/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