prime number checking algorithm

Solutions on MaxInterview for prime number checking algorithm by the best coders in the world

showing results for - "prime number checking algorithm"
Noemi
21 Mar 2019
1function isPrime(num) {
2  if (num <= 3) return num > 1;
3  
4  if ((num % 2 === 0) || (num % 3 === 0)) return false;
5  
6  let count = 5;
7  
8  while (Math.pow(count, 2) <= num) {
9    if (num % count === 0 || num % (count + 2) === 0) return false;
10    
11    count += 6;
12  }
13  
14  return true;
15}
16
Matteo
22 Nov 2016
1bool IsPrime(int n)
2{
3    if (n == 2 || n == 3)
4        return true;
5
6    if (n <= 1 || n % 2 == 0 || n % 3 == 0)
7        return false;
8
9    for (int i = 5; i * i <= n; i += 6)
10    {
11        if (n % i == 0 || n % (i + 2) == 0)
12            return false;
13    }
14
15    return true;
16}
17
Oskar
26 May 2019
1def is_prime(n: int) -> bool:
2    """Primality test using 6k+-1 optimization."""
3    if n <= 3:
4        return n > 1
5    if n % 2 == 0 or n % 3 == 0:
6        return False
7    i = 5
8    while i ** 2 <= n:
9        if n % i == 0 or n % (i + 2) == 0:
10            return False
11        i += 6
12    return True
13
similar questions
queries leading to this page
prime number checking algorithm