Added PrimeCheck (#340)

This commit is contained in:
Utkarsh Chaudhary
2020-10-02 00:06:07 +05:30
committed by GitHub
parent f9bac45d54
commit eaf4f91c01

29
Maths/PrimeCheck.js Normal file
View File

@ -0,0 +1,29 @@
/*
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/prime_check.py
Complexity:
O(sqrt(n))
*/
const PrimeCheck = (n) => {
// input: n: int
// output: boolean
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
return false
}
}
return true
}
const main = () => {
// PrimeCheck(1000003)
// > true
console.log(PrimeCheck(1000003))
// PrimeCheck(1000001)
// > false
console.log(PrimeCheck(1000001))
}
main()