Created
April 12, 2016 14:52
-
-
Save gabrieledarrigo/df35b799fed1e3e41dab3e17b01b1bb9 to your computer and use it in GitHub Desktop.
isPrime function
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
function isPrime(n) { | |
if (n === 1) { | |
return false; | |
} | |
if (n === 2) { | |
return true; | |
} | |
const root = Math.floor(Math.sqrt(n)); | |
for (let i = 2; i <= root; i++) { | |
if (n % i === 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167].forEach(n => { | |
console.log(n + ' is prime: ' + isPrime(n)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment