A prime number is a natural number greater than 1 that is divisible only by 1 and itself. In C++, a prime number can be checked using different approaches, ranging from simple brute-force methods to optimized techniques that reduce the number of divisibility checks.
- A prime number has exactly two positive divisors: 1 and the number itself
- Numbers less than or equal to 1 are not considered prime numbers
- Different approaches offer different time complexities and performance benefits
Examples:
Input: n = 29
Output: 29 is prime
Explanation: 29 has no divisors other than 1 and 29 itself. Hence, it is a prime number.Input: n = 15
Output: 15 is NOT prime
Explanation: 15 has divisors other than 1 and 15 (i.e., 3 and 5). Hence, it is not a prime number.
Approaches to Check Prime Numbers
A prime number can be checked using multiple approaches, ranging from simple brute-force methods to optimized algorithms with better time complexity.
Brute Force Method - O(n) Time
In this approach, we check how many numbers divide n exactly. If the count of divisors is greater than 2, the number is not prime; otherwise, it is prime. This method is simple to understand but inefficient for large values of n.
Approach
- If n <= 1, the number is not prime
- Traverse all numbers from 1 to n
- Count how many numbers divide n without leaving a remainder
- If the divisor count is greater than 2, the number is not prime
- Otherwise, the number is prime
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
cout << n << " is NOT prime";
else {
// Count the divisors of n
for (int i = 1; i <= n; i++) {
if (n % i == 0)
cnt++;
}
// If n is divisible by more than 2
// numbers then it is not prime
if (cnt > 2)
cout << n << " is NOT prime";
// else it is prime
else
cout << n << " is prime";
}
return 0;
}
Output
29 is prime
Explanation
- The loop checks every number from 1 to n to determine whether it divides n.
- The variable cnt stores the total number of divisors of n.
- A prime number has exactly two divisors: 1 and itself.
- Since 29 has only two divisors, the program prints "29 is prime".
Complexity Analysis
- Time Complexity: O(n) because the loop iterates from 1 to n.
- Space Complexity: O(1) as only a few extra variables are used.
Optimized Method - O(√n) Time
Instead of checking all numbers from 1 to n, we only check divisors up to √n. This optimization is based on the fact that if a number has a divisor greater than √n, it must also have a corresponding divisor smaller than √n.
Approach
- If n <= 1, the number is not prime
- Iterate from 2 to √n
- Check if any number divides n exactly
- If a divisor is found, the number is not prime
- Otherwise, the number is prime
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
cout << n << " is NOT prime";
else {
// Count the divisors of n
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
cnt++;
}
// If n is divisible by more than 2
// numbers then it is not prime
if (cnt > 0)
cout << n << " is NOT prime";
// else it is prime
else
cout << n << " is prime";
}
return 0;
}
Output
29 is prime
Explanation
- The loop runs only up to √n instead of checking all numbers up to n.
- If a divisor exists beyond √n, its corresponding pair divisor will be below √n.
- The variable cnt counts the number of divisors found in the range 2 to √n.
- Since no divisor is found for 29, the program prints "29 is prime".
Complexity Analysis
- Time Complexity: O(√n) because only numbers up to √n are checked.
- Space Complexity: O(1) as only a few auxiliary variables are used.
Further Optimization by Skipping Even Numbers - O(√n) Time
Since 2 is the only even prime number, all other even numbers are non-prime. Therefore, after handling the special case of 2, we only need to check odd divisors.
Approach
- If n <= 1, the number is not prime
- If n is even and greater than 2, the number is not prime
- Handle the special case when n = 2
- Check only odd divisors from 3 to √n
- If any divisor is found, the number is not prime
- Otherwise, the number is prime
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 29;
int cnt = 0;
// If number is less than/equal
// to 1 and number is even accept 2
// then it is not prime
if (n <= 1 || ((n > 2) && (n%2 == 0)))
cout << n << " is NOT prime";
else {
if (n == 2) {
cout << n << " is prime";
return 0;
} else {
// Check how many numbers divide
// n in the range 2 to sqrt(n)
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
cnt++;
}
// if cnt is greater than 0,
// then n is not prime
if (cnt > 0)
cout << n << " is NOT prime";
// else n is prime
else
cout << n << " is prime";
}
}
return 0;
}
Output
29 is prime
Explanation
- The program first eliminates all even numbers greater than 2.
- The loop increments by 2, checking only odd divisors.
- This reduces the number of iterations by nearly half compared to the previous method.
- Since no odd divisor divides 29, the program prints "29 is prime".
Complexity Analysis
- Time Complexity: O(√n) in the worst case.
- Space Complexity: O(1).
- Performs fewer divisibility checks than the basic √n approach, making it faster in practice.