Emirp is the word “prime” spelled backwards, and it refers to a prime number that becomes a new prime number when you reverse its digits. Emirps do not include palindromic primes (like 151 or 787) nor 1-digit primes like 7. 107, 113, 149, and 157 – reverse them and you’ve got a new prime number on your hands. Source: Wiki
Given a number n, the task is to print all Emrips smaller than or equal to n.
Input : n = 40 Output : 13 31 Input : n = 100 Output : 13 31 17 71 37 73 79 97
Below are the steps :
1) Use Sieve of Eratosthenes to generate all primes smaller than or equal to n. We can also use sieve of sundaram.
2) Traverse all generated prime numbers. For every traversed prime number print this number and its reverse if following conditions are satisfied.
………….a) If reverse is also prime.
………….b) Reverse is not same as prime (palindromes are not allowed)
………….c) Reverse is smaller than or equal to n.
Below is the implementation of above idea.
13 31 17 71 37 73 79 97
This article is contributed by Shivam Pradhan ( anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Check if given number is Emirp Number or not
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count numbers which can be constructed using two numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- 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
- Count numbers which are divisible by all the numbers from 2 to 10
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Check if a given pair of Numbers are Betrothed numbers or not
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count of numbers upto M divisible by given Prime Numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- How to swap two numbers without using a temporary variable?
- Lucky Numbers