Before writing any code or algorithm, it helps to be clear on what qualifies as prime and what doesn’t:
- Numbers less than or equal to 1 (n≤1n \le 1n≤1): Not prime. For example,
1only has one positive divisor (1itself), while0and negative integers do not meet the definition of a prime number. - Prime numbers (e.g.,
2,3,5,7,11): Greater than1and divisible only by1and themselves. Notice that2is the only even prime number. - Composite numbers (e.g.,
4,6,9,25): Greater than1with at least one divisor other than1and the number itself (for instance, 25=5×5, so5is a factor).
Example Walkthroughs
- Input:
n = 7→ Output:true7is greater than1and cannot be divided evenly by any integer between2and6. - Input:
n = 25→ Output:false25is divisible by5(25=5×5), so it is not prime. - Input:
n = 1→ Output:false1is not greater than1and has only a single positive divisor.
How a Prime Number Checker Works
To test if n is prime, you look for any integer d>1 that divides n with zero remainder (nmodd==0). If you find even one such divisor, n is not prime (false). If no divisor exists, n is prime (true).
1. Basic Trial Division (Up to n−1)
The most straightforward method checks every integer i from 2 up to n−1:
- If n≤1, return
false. - Loop i from 2 to n−1: if nmodi==0, return
false. - If the loop finishes without finding a factor, return
true.
While easy to understand, checking every number up to n−1 does extra work for larger inputs.
2. Square Root Optimization (Up to n
)
If a number n has a divisor larger than n, it must also have a corresponding divisor smaller than or equal to n
(since a×b=n means at least one of a or b is ≤n
).
Therefore, you only need to test divisors i where i×i≤n (or i≤n). This drops the time complexity from O(n) to O(n
).
3. Skipping Even Numbers / 6k±1 Optimization
You can make the n check even faster:
- Handle n≤1 immediately (
false). - Handle
2and3directly (true). - Eliminate multiples of
2and3right away (n % 2 == 0 || n % 3 == 0→false). - Every prime greater than
3takes the form 6k±1. Looping with step6(checking i and i+2 starting at i=5 while i×i≤n) tests roughly one-third as many candidates as checking every integer up to n.
Comparison of Approaches
| Approach | Divisor Range Tested | Time Complexity | Space Complexity | Best For |
|---|---|---|---|---|
| Naive Loop | 2 to n−1 | O(n) | O(1) | Learning the basic concept |
| Square Root Check | 2 to ⌊n | O(n | O(1) | Clean, standard interviews & scripts |
| 6k±1 Wheel Check | 5,7,11,13… up to ⌊n | O(n | O(1) | Faster single-number checks on large ints |
Read More Also – How Many Keys Are on a Computer Keyboard? Complete Key Breakdown Guide
Code Examples
Python
Python
def is_prime(n: int) -> bool:
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
# Examples
print(is_prime(7)) # True
print(is_prime(25)) # False
print(is_prime(1)) # False
JavaScript
JavaScript
function isPrime(n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 === 0 || n % 3 === 0) return false;
for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) {
return false;
}
}
return true;
}
// Examples
console.log(isPrime(7)); // true
console.log(isPrime(25)); // false
console.log(isPrime(1)); // false
C++
C++
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
int main() {
cout << boolalpha;
cout << isPrime(7) << "\n"; // true
cout << isPrime(25) << "\n"; // false
cout << isPrime(1) << "\n"; // false
return 0;
}
Common Pitfalls to Avoid
- Integer overflow in
i * i <= n: In languages with fixed-width integers (like 32-bitintin C++ or Java),i * ican overflow ifnis close toINT_MAX. Uselong long/longfor the loop counter or writei <= n / i(whenn > 0). - Treating
1or2incorrectly: Always test boundary inputs (-5,0,1,2,3,4,9,25,97) when verifying your function. - Checking many numbers one by one: If you need all prime numbers up to a limit N rather than testing a single number n, the Sieve of Eratosthenes is typically faster overall.
Read More Also – Understanding the HTML Color Code #F8F8FF (Ghost White): Complete Guide for Web Designers






















