Created
May 9, 2022 11:01
-
-
Save flandolf/57bb8bc86bb1d317c2a0140bd8025f72 to your computer and use it in GitHub Desktop.
primes
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
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