Mersenne Prime是质数,小于1的2的幂。换句话说,如果任何素数的形式为2 k -1,其中k是大于或等于2的整数,则它是Mersenne素数。前几个Mersenne素数是3、7、31和127。
该任务将打印所有小于输入正整数n的Mersenne Prime。
例子:
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
想法是使用Eratosthenes筛子生成小于或等于给定数n的所有素数。生成所有此类素数后,我们将迭代2 k -1形式的所有数字,并检查它们是否为素数。
以下是该想法的实现。
C++
// Program to generate mersenne primes
#include
using namespace std;
// Generate all prime numbers less than n.
void SieveOfEratosthenes(int n, bool prime[])
{
// Initialize all entries of boolean array
// as true. A value in prime[i] will finally
// be false if i is Not a prime, else true
// bool prime[n+1];
for (int i=0; i<=n; i++)
prime[i] = true;
for (int p=2; p*p<=n; p++)
{
// If prime[p] is not changed, then it
// is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
}
// Function to generate mersenne primes less
// than or equal to n
void mersennePrimes(int n)
{
// Create a boolean array "prime[0..n]"
bool prime[n+1];
// Generating primes using Sieve
SieveOfEratosthenes(n,prime);
// Generate all numbers of the form 2^k - 1
// and smaller than or equal to n.
for (int k=2; ((1<
Java
// Program to generate
// mersenne primes
import java.io.*;
class GFG {
// Generate all prime numbers
// less than n.
static void SieveOfEratosthenes(int n,
boolean prime[])
{
// Initialize all entries of
// boolean array as true. A
// value in prime[i] will finally
// be false if i is Not a prime,
// else true bool prime[n+1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed
// , then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
}
// Function to generate mersenne
// primes lessthan or equal to n
static void mersennePrimes(int n)
{
// Create a boolean array
// "prime[0..n]"
boolean prime[]=new boolean[n + 1];
// Generating primes
// using Sieve
SieveOfEratosthenes(n, prime);
// Generate all numbers of
// the form 2^k - 1 and
// smaller than or equal to n.
for (int k = 2; (( 1 << k) - 1) <= n; k++)
{
long num = ( 1 << k) - 1;
// Checking whether number is
// prime and is one less then
// the power of 2
if (prime[(int)(num)])
System.out.print(num + " ");
}
}
// Driven program
public static void main(String args[])
{
int n = 31;
System.out.println("Mersenne prime"+
"numbers smaller than"+
"or equal to "+n);
mersennePrimes(n);
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Program to generate mersenne primes
# Generate all prime numbers
# less than n.
def SieveOfEratosthenes(n, prime):
# Initialize all entries of boolean
# array as true. A value in prime[i]
# will finally be false if i is Not
# a prime, else true bool prime[n+1]
for i in range(0, n + 1) :
prime[i] = True
p = 2
while(p * p <= n):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == True) :
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
# Function to generate mersenne
# primes less than or equal to n
def mersennePrimes(n) :
# Create a boolean array
# "prime[0..n]"
prime = [0] * (n + 1)
# Generating primes using Sieve
SieveOfEratosthenes(n, prime)
# Generate all numbers of the
# form 2^k - 1 and smaller
# than or equal to n.
k = 2
while(((1 << k) - 1) <= n ):
num = (1 << k) - 1
# Checking whether number
# is prime and is one
# less then the power of 2
if (prime[num]) :
print(num, end = " " )
k += 1
# Driver Code
n = 31
print("Mersenne prime numbers smaller",
"than or equal to " , n )
mersennePrimes(n)
# This code is contributed
# by Smitha
C#
// C# Program to generate mersenne primes
using System;
class GFG {
// Generate all prime numbers less than n.
static void SieveOfEratosthenes(int n,
bool []prime)
{
// Initialize all entries of
// boolean array as true. A
// value in prime[i] will finally
// be false if i is Not a prime,
// else true bool prime[n+1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
}
// Function to generate mersenne
// primes lessthan or equal to n
static void mersennePrimes(int n)
{
// Create a boolean array
// "prime[0..n]"
bool []prime = new bool[n + 1];
// Generating primes
// using Sieve
SieveOfEratosthenes(n, prime);
// Generate all numbers of
// the form 2^k - 1 and
// smaller than or equal to n.
for (int k = 2; (( 1 << k) - 1) <= n; k++)
{
long num = ( 1 << k) - 1;
// Checking whether number is
// prime and is one less then
// the power of 2
if (prime[(int)(num)])
Console.Write(num + " ");
}
}
// Driven program
public static void Main()
{
int n = 31;
Console.WriteLine("Mersenne prime numbers"
+ " smaller than or equal to " + n);
mersennePrimes(n);
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出:
Mersenne prime numbers smaller than or equal to 31
3 7 31