Mersenne Prime is a prime number that is one less than a power of two. In other words, any prime is Mersenne Prime if it is of the form 2k-1 where k is an integer greater than or equal to 2. First few Mersenne Primes are 3, 7, 31 and 127.
The task is print all Mersenne Primes smaller than an input positive integer n.
Input: 10 Output: 3 7 3 and 7 are prime numbers smaller than or equal to 10 and are of the form 2k-1 Input: 100 Output: 3 7 31
The idea is to generate all the primes less than or equal to the given number n using Sieve of Eratosthenes. Once we have generated all such primes, we iterate through all numbers of the form 2k-1 and check if they are primes or not.
Below is the implementation of the idea.
Mersenne prime numbers smaller than or equal to 31 3 7 31
This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Print the nearest prime number formed by adding prime numbers to N
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Prime numbers after prime P with sum S
- Sub array sum is prime or not
- Prime or not in Python
- Pierpont Prime
- Sexy Prime
- Prime Numbers
- Sum of the first N Prime numbers
- Prime String
- Balanced Prime
- Prime number in PL/SQL