Given a number N, print all prime number smaller than or equal to N in reverse order .
For example, if N is 9, the output should be “7, 5, 3, 2”.
Input : N = 5 Output : 5 3 2 Input : N = 20 Output : 19 17 13 11 7 5 3 2
A simple solution is to traverse from N to 1. For every number, check if it is a prime using school method to check for prime. Print the number if it is prime.
An efficient solution is to use Sieve of Eratosthenes. We efficiently find all number from 1 to N, then print them.
Prime number in reverse order 23 19 17 13 11 7 5 3 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Program to print numbers from N to 1 in reverse order
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Print characters having prime frequencies in order of occurrence
- Sort only non-prime numbers of an array in increasing order
- Sort prime numbers of an array in descending order
- Prime numbers after prime P with sum S
- Check if a prime number can be expressed as sum of two Prime Numbers
- Sum of prime numbers without odd prime digits
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Count all prime numbers in a given range whose sum of digits is also prime
- Print all numbers in given range having digits in strictly increasing order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.