Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To Check Whether a Number is a Mersenne Number or Not in Java?
What is a Mersenne Number?
A Mersenne number is a positive integer that is obtained using the expression M(n)= 2n-1, where 'n' is an integer. If you keep any value of n (e.g., 0, 1, 2, 3) in the above expression, the result will be a Mersenne number.
For example, consider n = 2 and calculate the expression 22 - 1. The result is 3, which is a Mersenne number. Not all Mersenne numbers are prime (e.g., 24 - 1 = 15) is a mersenne number, which is not prime.
Here are some other examples of Mersenne numbers:
- 23 - 1 = 7
- 24 - 1 = 15
- 25 - 1 = 31
- 26 - 1 = 63
To find the next Mersenne number, you just need to multiply 2 by the current Mersenne number and add 1.
Input & Output Scenarios
Below are some input and output scenarios that provide a clear understanding of the problem and calculation to check the Mersenne number:
Scenario 1
Suppose the input number is 3:
<strong>Input:</strong> 3 <strong>Output:</strong> Yes <strong>Calculation: </strong>M(n) = M(3) = 2<sup>n</sup>-1 = (2^3)-1 = 7
As you can see, the result of expression 2n-1 is a prime number (i.e., 7). Hence, 3 is a Mersenne number.
Scenario 2
Let's suppose the given number is 4:
<strong>Input:</strong> 304 <strong>Output:</strong> No <strong>Calculation:</strong> For any value of 'n' we never get the 2<sup>n</sup>-1 as 304.
The number 304 is not a Mersenne number.
Since we need to calculate the power of any number raised to the power of another number, we will use an inbuilt java.lang.Math.pow() method. Here is the syntax:
power = Math.pow(inputValue, 2)
Here, inputValue is the value of which power that will be calculated.
Example 1
The following is a basic example of checking whether the number 3 is a Mersenne number or not by comparing the value of the expression 2n-1 with the given number:
public class checkMersenne {
public static void main(String args[]) {
int num = 3;
System.out.println("The given number is: " + num);
//increase the value of num by one and assign it into another temp variable
int temp = num + 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++) {
//calculate the power value
p = (int)Math.pow(2, i);
if(p > temp) {
break;
}
else if(p == temp) {
System.out.println("Yes! " + num +" is a Mersenne number.");
res = 1;
}
}
if(res == 0) {
System.out.println("No! " + num + " is not a Mersenne number.");
}
}
}
The above program produces the following output:
The given number is: 3 Yes! 3 is a Mersenne number.
Example 2
In the example below, we define a method named isMersenneNumber(), which calculates the power of the number and compares the expression 2n - 1 with the given number 10 to check whether the number is Mersenne:
import java.util.Scanner;
public class checkMersenne {
//method to calculate the Mersenne number
public static boolean isMersenneNumber(int num){
int temp = num + 1, p = 0;
//loop will stop only when break statement is used...
for(int i = 0; ;i++) {
//calculate the power value
p = (int)Math.pow(2, i);
if(p > temp) {
break;
}
else if(p == temp) {
return true;
}
}
return false;
}
public static void main(String args[]) {
int num = 10;
System.out.println("The given number is: " + num);
//calling the isMersenneNumber() to check
System.out.println("Is the number " + num + " is a Mersenne? " + isMersenneNumber(num));
}
}
Below is the output of the above program:
The given number is: 10 Is the number 10 is a Mersenne? false
