Check if number is a prime number
6 November 2022 (Updated 6 November 2022)
function isPrimeNumber(num: number): boolean {
if (num <= 1) {
return false
}
for (let n = 2; n < num; n++) {
// If the number is divisible by any number other than itself and 1,
// it's not a prime so return false
if (num % n === 0) {
return false
}
}
return true
}
Tagged:
Snippets
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment