PROGRAM : MERSENNE NUMBER
A number is said to be mersenne number if it is one less than a power of 2. Example- 7 is a mersenne number as it is 2^3-1.Similarly 1023 is a mersenne number as it is 2^10-1.
SYNTAX
import java.io.*;
class mersenne
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,m;
boolean flag=false;
System.out.println("Enter a number to check mersenne number");
n=Integer.parseInt(br.readLine());
m=n+1;
for(int i=0;i<=n;i++)
{
if((Math.pow(2,i)==m))
{
flag=true;
}
}
if(flag==true)
{
System.out.println("The Number "+ (m-1)+" is Mersenne Number");
}
else
System.out.println("The Number "+ (m-1)+" is not a Mersenne Number");}
}
Comments
Post a Comment