Skip to content

Instantly share code, notes, and snippets.

@flandolf
Created May 9, 2022 11:01
Show Gist options
  • Save flandolf/57bb8bc86bb1d317c2a0140bd8025f72 to your computer and use it in GitHub Desktop.
Save flandolf/57bb8bc86bb1d317c2a0140bd8025f72 to your computer and use it in GitHub Desktop.
primes
const fs = require("fs");
var i = 0;
var goto = 10000 + 1;
//see if primes.txt exists if it does, delete it
if (fs.existsSync("primes.txt")) {
fs.unlinkSync("primes.txt");
}
//same for notprimes.txt
if (fs.existsSync("notprimes.txt")) {
fs.unlinkSync("notprimes.txt");
}
while (true) {
if (i != goto) {
if (isPrime(i)) {
console.log(`${i.toString()} is prime out of ${goto - 1} numbers` );
//append this to a file
fs.appendFileSync("primes.txt", `${i.toString()}\n`);
} else {
console.log(
`${i.toString()} is not prime out of ${goto - 1} numbers`
);
fs.appendFileSync("notprimes.txt", `${i.toString()}\n`);
}
i++;
} else {
if (i > goto) {
exit(0);
break;
}
}
}
function isPrime(num) {
for (var i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return num > 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment