-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.js
31 lines (29 loc) · 917 Bytes
/
primes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env node
var fs = require('fs');
var outfile = "primes.txt";
var out = "2";
var primes=0;
var isprime = true;
var stack = new Array();
stack = [];
for (var counter = 3; counter <= 542; counter = counter + 1)
{
// For now, we believe that it is a prime
isprime = true;
var limit = Math.round(Math.sqrt(counter)); // See comment from @AresAvatar, below
// We try to find a number between 2 and limit that gives us a reminder of 0
for (var mod = 2; mod <= limit; mod++) {
// If we find one, we know it's not a prime
if (counter%mod == 0) {
isprime = false;
break; // Break out of the inner for loop
}
}
if (isprime) {
out += "," + counter
console.log(counter, limit);
primes = primes + 1;
}
}
fs.writeFileSync(outfile, out);
console.log("Script: " + __filename + "\nWrote: " + out + "To: " + outfile);