Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Last active July 29, 2020 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalconceptvisuals/620f453f202f04d9358b2f163d25eef8 to your computer and use it in GitHub Desktop.
Save digitalconceptvisuals/620f453f202f04d9358b2f163d25eef8 to your computer and use it in GitHub Desktop.
// Check if given number is prime
const isPrime = number => {
// Divide number by all numbers from 2 to sqrt(number)
// If divisible, then its not a prime
let div;
for (div = 2; div < Math.sqrt(number); div++)
if (number % div == 0) {
console.log(`Looped ${div} times`);
return false;
}
console.log(`Looped ${div} times`);
return true;
}
// This is a known prime
console.log(isPrime(1299827));
console.log(isPrime(1299827));
/* Output
Looped 1141 times
1299827 is prime: true
Looped 1141 times
1299827 is prime: true
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment