Print prime numbers from 1 to N in reverse order



In this article, we will learn how to print prime numbers from 1 to N in reverse order. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. Our goal is to find all prime numbers in the given range from 1 to N and then print them in reverse order.

For example, given N = 20, the prime numbers between 1 and 20 are:

2, 3, 5, 7, 11, 13, 17, 19

The output should be the prime numbers printed in reverse order:

19, 17, 13, 11, 7, 5, 3, 2 

We will show you how to easily find and print prime numbers from 1 to N in reverse order using C++.

Ways to Print Prime Numbers in Reverse Order

There are a few different ways to print prime numbers from 1 to N in reverse order. We will look at the following approaches:

Using a Simple Loop and Check for Primes

In this approach, we use a basic loop to check each number from N down to 1 and determine whether it's a prime number. For each number, we check if it has divisors other than 1 and itself, and if it's prime, we print it.

Example

In this example, we start from N and loop down to 1. For each number, we check if it's prime. If it is, we print it, showing prime numbers in reverse order from N to 1.

#include <iostream>
#include <cmath>
using namespace std;

// Check if number is prime
bool isPrime(int num) {
    if (num <= 1) return false; // Not prime if <= 1
    for (int i = 2; i <= sqrt(num); ++i) { // Check divisibility up to sqrt(num)
        if (num % i == 0) return false; // Not prime if divisible
    }
    return true; // Prime if no divisors found
}

int main() {
    int N;
    cout << "Enter a number N: ";
    cin >> N;

    // Print primes from N down to 1
    for (int i = N; i >= 1; --i) {
        if (isPrime(i)) {
            cout << i << " "; // Output prime number
        }
    }
    return 0;
}

The output displays the prime numbers starting from the user input (e.g., 20) and prints them in reverse order, from N down to 1.

Enter a number N: 20
19 17 13 11 7 5 3 2 

Time Complexity: O(N * sqrt(N)), because the outer loop runs up to sqrt(N) and the inner loop marks multiples of each prime, leading to O(N * sqrt(N)) complexity.

Space Complexity: O(1), only a fixed amount of extra space is used, regardless of the input size.

Using the Sieve of Eratosthenes

The Sieve of Eratosthenes is an efficient algorithm for finding all prime numbers up to a given number N. In this approach, we first find all the primes up to N using the sieve method, and then print them in reverse order.

Example

In this example, we use the Sieve of Eratosthenes to find all primes up to N and print them in reverse order by marking non-prime numbers and looping through the array.

#include <iostream>
#include <vector>
using namespace std;

void sieve(int N) {
    // Mark all numbers as prime initially
    vector<bool> isPrime(N + 1, true); 
    // 0 and 1 are not prime
    isPrime[0] = isPrime[1] = false; 

    for (int i = 2; i * i <= N; ++i) { 
        if (isPrime[i]) {
            // Mark multiples of i as not prime
            for (int j = i * i; j <= N; j += i) { 
                isPrime[j] = false;
            }
        }
    }
    // Print primes from N to 2
    for (int i = N; i >= 2; --i) { 
        if (isPrime[i]) {
            cout << i << " "; 
        }
    }
}

int main() {
    int N;
    cout << "Enter a number N: ";
    cin >> N;
    
    sieve(N); // Find and print primes
    return 0;
}

The output displays the prime numbers from the user input N, and prints them in reverse order, starting from N down to 2.

Enter a number N: 20
19 17 13 11 7 5 3 2 

Time Complexity: O(N * log(log(N))) because the algorithm goes through numbers up to sqrt(N) and for each prime number, it marks its multiples as not prime.

Space Complexity: O(N) because the algorithm uses an array to store prime status for all numbers up to N.

Using a Reverse Loop After Finding Primes

This method first finds all prime numbers up to N using a loop. Once the primes are identified, they are stored in a vector. Finally, the primes are printed in reverse order, starting from N and going down to 2.

Example

In this example, we find primes up to N, store them in a vector, and print them in reverse order.

#include <iostream>
#include <vector>
using namespace std;

// Function to check if a number is prime
bool isPrime(int num) {
    // Numbers <= 1 are not prime
    if (num <= 1) return false; 
    // Check divisibility up to sqrt(num)
    for (int i = 2; i * i <= num; ++i) { 
        // If divisible, num is not prime
        if (num % i == 0) return false; 
    }
    // Return true if no divisors found
    return true; 
}

int main() {
    int N;
    cout << "Enter a number N: ";
    cin >> N;

    vector<int> primes; // Vector to store prime numbers
    for (int i = 2; i <= N; ++i) {
        if (isPrime(i)) {
             // Add prime numbers to the vector
            primes.push_back(i);
        }
    }

    // Print prime numbers in reverse order
    for (int i = primes.size() - 1; i >= 0; --i) {
        cout << primes[i] << " "; 
    }
    return 0;
}

In this output, the program prints all prime numbers up to N, where N is provided by the user, in reverse order.

Enter a number N: 20
19 17 13 11 7 5 3 2 

Time Complexity: O(N * sqrt(N)) because each number is checked for prime factors up to its square root.

Space Complexity: O(N) because the prime numbers are stored in a vector.

Conclusion

In this article, we discussed methods to print prime numbers from 1 to N in reverse order, including using a simple loop, the Sieve of Eratosthenes, and a reverse loop. The Sieve of Eratosthenes is most efficient for large N. Choose the method based on input size and efficiency needs.

Updated on: 2025-02-21T16:29:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements