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 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.
- Print the nearest prime number formed by adding prime numbers to N
- Quick ways to check for Prime and find next Prime in Java
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Find coordinates of a prime number in a Prime Spiral
- Check whether the sum of prime elements of the array is prime or not
- Sum of each element raised to (prime-1) % prime
- 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
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of the first N Prime numbers
- Sexy Prime
- Prime number in PL/SQL
- Prime Numbers