
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alternate Primes till N in C++?
A prime number is a number greater than 1 that is divisible only by 1 and itself. Given a number N, our task is to print all alternate prime numbers up to N. This means we skip every second prime number and only print the 1st position, 3rd position, 5th position, and so on.
Let's look at the example scenarios to understand the problem clearly:
Scenario 1
Input: N = 15 Output: 2 5 11 Explanation: Prime numbers up to 15 are: 2, 3, 5, 7, 11, 13 Taking alternate primes (1st, 3rd, 5th): 2, 5, 11
Scenario 2
Input: N = 30 Output: 2 5 11 17 23 Explanation: Prime numbers up to 30 are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 Taking alternate primes: 2, 5, 11, 17, 23
To print only the alternate prime numbers in C++, we will see both the brute force and the optimized approaches:
Brute Force Approach
In the brute force approach, we check each number from 2 up to a given number N and see if it's divisible by any number up to its square root. If not, it's prime and we add it to a list. After collecting all the primes, we print alternate ones by picking one, skipping the next, and so on.
Example
Following is a C++ program that finds all primes up to a limit, stores them in a list, and prints the alternate ones.
#include <iostream> #include <vector> #include <cmath> using namespace std; // Check if a number is prime bool isPrime(int number) { if (number < 2) return false; for (int divisor = 2; divisor <= sqrt(number); divisor++) { if (number % divisor == 0) return false; } return true; } // Generate and print alternate prime numbers void displayAlternatePrimes(int limit) { vector<int> primes; for (int current = 2; current <= limit; current++) { if (isPrime(current)) { primes.push_back(current); } } cout << "Input: " << limit << endl; cout << "Output: "; for (int i = 0; i < primes.size(); i += 2) { cout << primes[i] << " "; } cout << endl; } int main() { int numberLimit = 30; displayAlternatePrimes(numberLimit); return 0; }
Below is the output of the above program, where we display alternate prime numbers up to the given input.
Input: 30 Output: 2 5 11 17 23
Time Complexity: O(nsqrt(n)) because we go through each number up to n and check if it's prime by dividing it up to its square root.
Space Complexity: O(n / log n) because we save all the prime numbers up to n in a list.
Using Sieve of Eratosthenes(Optimized)
The Sieve of Eratosthenes method finds prime numbers by removing non-prime numbers. Instead of checking each number one by one, we use a boolean array to mark non-prime numbers and eliminate the multiples of each prime as we go through the array.
Algorithm
Following is an algorithm:
- First, we create a boolean array of size N + 1 and mark all values as true, assuming all numbers are prime.
- Next, starting from 2, for each number still marked true, we mark all of its multiples as false.
- We continue this process until we reach the square root of N. After the loop, the numbers still marked true in the array are the prime numbers.
- Finally, we go through this list and print every alternate prime number.
Example
Here's a C++ program where we follow the above steps to generate prime numbers and then print the alternate ones from the result.
#include <iostream> #include <vector> using namespace std; void displayAlternatePrimesUpToLimit(int limit) { vector<bool> isPrimeNumber(limit + 1, true); isPrimeNumber[0] = false; isPrimeNumber[1] = false; // Mark non-prime numbers using the Sieve of Eratosthenes for (int current = 2; current * current <= limit; current++) { if (isPrimeNumber[current]) { for (int multiple = current * current; multiple <= limit; multiple += current) isPrimeNumber[multiple] = false; } } vector<int> primeNumbers; // Collect all prime numbers into a list for (int number = 2; number <= limit; number++) { if (isPrimeNumber[number]) primeNumbers.push_back(number); } // Print every second (alternate) prime number cout << "Input: " << limit << endl; cout << "Output: "; for (int i = 0; i < primeNumbers.size(); i += 2) cout << primeNumbers[i] << " "; cout << endl; } int main() { int upperLimit = 30; displayAlternatePrimesUpToLimit(upperLimit); return 0; }
Following is the output of the above program:
Input: 30 Output: 2 5 11 17 23
Time Complexity: O(n log log n) because we use the Sieve method to find prime numbers quickly.
Space Complexity: O(n) because we use a boolean list to track primes and another list to store them.
Conclusion
In this article, we learned how to print alternate prime numbers using two methods. First, we used a brute force approach by checking each number for primality through divisibility. Then, we used the Sieve of Eratosthenes, where we marked non-prime numbers using a boolean array. This method is more efficient with a time complexity of O(n log log n) and space complexity of O(n).