How js verifies whether a number is a prime
This article mainly introduces js how to verify whether a number is a prime number, the article is very detailed, has a certain reference value, interested friends must read it!
Verify whether a number is a prime
1. If the number is 2 or 3, it must be prime
2. If it is an even number, it must not be a prime
3. If this number is not divisible by 3mure-any of its square roots, m must be a prime. And the divisor can be incremented each time (excluding even numbers)
Function isPrime (num) {
If (num = 2 | | num = 3) {
Return true
}
If (num% 2 = = 0) {
Return false
}
Let divisor = 3 limit = Math.sqrt (num)
While (limit > = divisor) {
If (num% divisor = = 0) {
Return false
}
Else {
Divisor + = 2
}
}
Return true
}
Console.log (isPrime (30)); / / false
The above is all the content of the article "how to verify whether a number is a prime by js". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!