How To Check Whether a Number is a Mersenne Number or Not in Java?


A prime number is said to be a Mersenne number, if the number satisfy the expression of M(n)= 2n-1, where ‘n’ is an integer.

Let, ‘n’ is an integer. If 2n -1 is equal to the prime number, then that number is known as mersenne number

Some examples of Mersenne numbers are: 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095 ... etc

In this article, we will see how to check if a number is a Mersenne number by using Java programming language.

To show you some instances

Instance-1

Input number is 15.

Let’s check it by using the logic of Mersenne number.

If we take n = 4.

Then M(n)= M(4)= 2n-1 = (2^4)-1= 15.

As we notice here, taking the n value as 4, 2n – 1 is equal to the original prime number

Hence, 15 is a Mersenne number.

Instance-2

Input number is 127.

Let’s check it by using the logic of Mersenne number.

If we take n = 7.

Then M(n)= M(7)= 2n-1 = (2^7)-1= 127.

As we notice here, taking the n value as 7, 2n-1 is equal to the original prime number.

Hence, 127 is a Mersenne number.

Instance-3

Input number is 304.

Let’s check it by using the logic of Mersenne number.

For any value of ‘n’ we never get the 2n-1 as 304.

Hence, 304 is not a Mersenne number.

Algorithm

Step-1 - Get the input number by static input method.

Step-2 - Take a loop to check whether for any value of ‘n’ we get the 2n -1 value same as the given number or not.

Step-3 - If we get any value then print the number is mersenne number.

Step-4 - If we do not get any value then print the number is not a mersenne number.

Syntax

To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the methoddouble-

power = Math.pow (inputValue,2)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Input Value

  • By Using User Input Value

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, we declare a variable and initialize it with a number. Then by using the algorithm we can check whether the number is a Mersenne number or not.

Example

import java.util.Scanner;
import java.math.BigInteger;
public class Main {
   public static void main(String args[]) {
 
      //declare a variable and initialize the value
      int inputNumber = 65535;
 
      //increase the value of inputNumber by one and assign it into another temp variable
      int temp = inputNumber + 1;
      int p = 0, res = 0;
   
      //loop to check whether by taking any value of n we are getting the original number or not
      for(int i=0; ;i++) {
      
         //for every iteration calculate the power value
         p = (int)Math.pow(2, i);

         //return false while the p value is greater than temp value
         if(p > temp) {
            break;
         }
         else if(p == temp) {
            System.out.println(inputNumber+" is a Mersenne number.");
            res = 1;
         }
      }
      if(res == 0) {
         System.out.println(inputNumber+" is not a Mersenne number.");
      }
   }
} 

Output

65535 is a Mersenne number.

Approach-2: By Using User Input Value

In this approach, we declare a variable and take a number as value from the user. Then by using the algorithm we can check whether the number is a Mersenne number or not.

Example

import java.util.Scanner;
import java.math.BigInteger;
public class Main {
   public static void main(String args[]) {
   
      //create object of Scanner class
      Scanner sc = new Scanner(System.in);
 
      //ask the user to enter a value
      System.out.print("Enter a number: ");
 
      //declare a variable and store the value by user input
      int inputNumber = sc.nextInt();
 
      //increase the value of inputNumber by one and assign it into another temp variable
      int temp = inputNumber + 1;
      int p = 0, res = 0;
   
      //loop to check whether by taking any value of n we are getting the original number or not
      for(int i=0; ;i++) {
      
         //for every iteration calculate the power value
         p = (int)Math.pow(2, i);
      
         //return false while the p value is greater than temp value
         if(p > temp) {
            break;
         }
         else if(p == temp) {
            System.out.println(inputNumber+" is a Mersenne number.");
            res = 1;
         }
      }
      if(res == 0) {
         System.out.println(inputNumber+" is not a Mersenne number.");
      }
   }
} 

Output

Enter a number: 3
3 is a Mersenne number.

Approach-3: By Using User Defined Method

In this approach, we declare a variable and initialize it with a number. Then call a user defined method by passing this number as parameter, then inside the method by using the algorithm we can check whether the number is a Mersenne number or not.

Example

import java.util.Scanner;
import java.math.BigInteger;
public class Main {
   
   //main method
   public static void main(String args[]) {
 
      //declare a variable and initialize the value
      int inputNumber = 3;
      
      //call the user defined method
      if(checkMersenne(inputNumber))
         System.out.println(inputNumber+" is a Mersenne number.");
      else
         System.out.println(inputNumber+" is not a Mersenne number.");
   }
   
   //user defined method to check mersenne number
   public static boolean checkMersenne(int n) { 
      
      //increase the value of inputNumber by one and assign it into a temp variable
      int temp = n + 1;
      int p = 0, res = 0;
      
      //loop to check whether by taking any value of n we are getting the original number or not
      for(int i=0; ;i++) {
         
         //for every iteration calculate the power value
         p = (int)Math.pow(2, i);
         
         //return false while the p value is greater than temp value
         if(p > temp) {
            break;
         }
         else if(p == temp) {
            res = 1;
            return true;
         }
      }
      if(res == 0) {
         return false;
      }
      return false;
   }
} 

Output

3 is a Mersenne number.

In this article, we explored how to check a number whether it is a Mersenne number or not in Java by using different approaches.

Updated on: 09-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements