forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeCheck.js
More file actions
Latest commit
25 lines (21 loc) · 446 Bytes
/
Copy pathPrimeCheck.js
File metadata and controls
25 lines (21 loc) · 446 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/prime_check.py
Complexity:
O(sqrt(n))
*/
constPrimeCheck=(n)=>{
// input: n: int
// output: boolean
if(n===1)returnfalse
if(n===0)returnfalse
if(n===2)returntrue
if(n%2===0)returnfalse
for(leti=3;i*i<=n;i+=2){
if(n%i===0){
returnfalse
}
}
returntrue
}
export{PrimeCheck}