## Prime Number Program in C++

Now that we understand the basic algorithm for checking prime numbers, let's refine our approach for better efficiency. One key improvement is to only check divisors up to the square root of the target number. Why the square root? Because if a number is divisible by another number larger than its square root, the resulting quotient would be less than the square root, meaning we would have already considered it as a divisor. This significantly reduces the number of iterations, especially for large numbers.

Here's how we can implement this optimized approach in a C++ program:

### C++

`#include <iostream>`

#include <cmath> // For the sqrt function

using namespace std;

bool isPrimeOptimized(int num) {

if (num <= 1) return false; // Numbers less than or equal to 1 are not prime

if (num == 2) return true; // 2 is a prime number

if (num % 2 == 0) return false; // Exclude even numbers greater than 2

for (int i = 3; i <= sqrt(num); i += 2) { // Only check odd numbers up to the sqrt of num

if (num % i == 0) {

return false; // Found a divisor, not prime

}

}

return true; // No divisors found, it's prime

}

int main() {

int number;

cout << "Enter a number to check: ";

cin >> number;

if (isPrimeOptimized(number)) {

cout << number << " is a prime number.";

} else {

cout << number << " is not a prime number.";

}

return 0;

}

You can also try this code with Online C++ Compiler

Run Code
Output

```
Enter a number to check: 56
56 is not a prime number
```

In this version, isPrimeOptimized first handles special cases: numbers less than 2, the number 2 itself, and even numbers greater than 2. Then, it iterates from 3 up to the square root of the number, checking only odd divisors. This is because, after excluding 2, no prime number can be even.

This program is more efficient than our first example and can quickly determine if a number is prime, even for larger integers. It's a great example of how a small change in the algorithm can lead to significant improvements in performance.

## Complexity Analysis

When we talk about complexity analysis in programming, we're looking at how efficiently a program runs, especially as the size of its input grows. For our prime number checks in C++, this boils down to understanding how many steps our program takes to confirm if a number is prime, depending on the size of that number.

### Basic Approach

In our first example, where we checked every number up to n - 1 to see if n is prime, the complexity is straightforward. For each number n, we perform n - 2 checks (since we start at 2 and go up to n - 1). This gives us a time complexity of O(n). In simpler terms, if the number to be checked doubles, the time it takes to check also doubles. This linear relationship is easy to understand but not efficient for large numbers.

### Optimized Approach

The second example, where we only check up to the square root of n, significantly reduces the number of steps. Instead of checking n - 2 numbers, we check approximately sqrt(n) numbers. This change brings our complexity down to O(sqrt(n)). In practice, this means that even if the number to check becomes four times larger, the time it takes to check it only doubles. This is a substantial improvement, especially when dealing with very large numbers.

Understanding complexity is crucial because it helps us write programs that run efficiently, even with large inputs. In the context of prime numbers, moving from a basic to an optimized approach can make the difference between a program that runs in a reasonable time and one that takes far too long to be practical.

## Frequently Asked Questions

### Why do we only check up to the square root of a number to determine if it's prime?

Checking up to the square root simplifies the process because if a number has a factor greater than its square root, the corresponding factor would be less than the square root, and we would have already encountered it.

### Can even numbers be prime?

Except for the number 2, which is the only even prime number, no other even numbers can be prime. This is because all even numbers greater than 2 can be divided by 2, which means they have at least one divisor other than 1 and themselves.

### How do you write a prime number in C++?

Prime number in C++ is a number greater than 1 that is divisible only by 1 and itself, often checked using loops.

### What is the function for prime number checker in C++?

A prime number checker function in C++ loops from 2 to âˆšn, checking if the number is divisible by any number other than 1 and itself.

## Conclusion

In this article, we've taken a thorough look into prime numbers in C++, starting from a basic understanding to developing an efficient program for prime number checks. We began by exploring what prime numbers are and why they're important, followed by a basic algorithm for checking prime numbers. We then enhanced our approach to create an optimized C++ program that efficiently determines if a number is prime by checking divisors only up to the square root and excluding even numbers.

You can refer to our __guided paths__ on the Code360. You can check our course to learn more about __DSA__, __DBMS__, __Competitive Programming__, __Python__, __Java__, __JavaScript,__ etc. Also, check out some of the __Guided Paths__ on topics such as __Data Structure and Algorithms__, __Competitive Programming__, __Operating Systems__, __Computer Networks,__ __DBMS__, __System Design__, etc., as well as some __Contests, ____Test Series__, and __Interview Experiences__ curated by top Industry Experts.