import java.util.*;
public class mersenne_number
{
static boolean prime_check(double n)
{
int count = 0;
for(int i=1;i<n;i++)
{
if(n%i==0)
count++;
}
if(count==1)
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter till where mersenne number is to be generated");
int n = sc.nextInt();
double a[] = new double[100];// storing the answers
int count = 0;
for(int i=2;i<n;i++)
{
double number = Math.pow(2, i)-1; // 2^k-1
if(number<=n)
{
boolean result = prime_check(number);
if(result==true)
{
a[count++] = number;
}
}
else
{
break;
}
}
if(count!=0)
{
System.out.println("The Mersenne number are as follows :");
for(int i=0;i<count;i++)
{
System.out.println(a[i]);
}
}
else
System.out.println("There are no mersenne number");
}
}
/*
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 2^k-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.
Examples:
Input: 10
Output: 3 7
3 and 7 are prime numbers smaller than or
equal to 10 and are of the form 2^k-1
Input: 100
Output: 3 7 31
*/
No comments:
Post a Comment