如何在Java中檢查一個數是否為梅森素數?
如果一個素數滿足表示式M(n)= 2n-1,其中‘n’為整數,則稱其為梅森素數。
假設‘n’為整數。如果2n -1 等於素數,則該數稱為梅森素數。
一些梅森素數的例子是:0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095 ...等等
在這篇文章中,我們將學習如何使用Java程式語言來檢查一個數是否為梅森素數。
舉幾個例子
示例1
輸入數字為15。
讓我們使用梅森素數的邏輯來檢查它。
如果我們取n = 4。
那麼M(n)= M(4)= 2n-1 = (2^4)-1= 15。
正如我們在這裡看到的,當n的值為4時,2n – 1等於原始素數。
因此,15是一個梅森素數。
示例2
輸入數字為127。
讓我們使用梅森素數的邏輯來檢查它。
如果我們取n = 7。
那麼M(n)= M(7)= 2n-1 = (2^7)-1= 127。
正如我們在這裡看到的,當n的值為7時,2n-1等於原始素數。
因此,127是一個梅森素數。
示例3
輸入數字為304。
讓我們使用梅森素數的邏輯來檢查它。
對於任何‘n’的值,我們都無法得到2n-1等於304。
因此,304不是一個梅森素數。
演算法
步驟1 - 透過靜態輸入方法獲取輸入數字。
步驟2 - 使用迴圈檢查是否存在任何‘n’的值使得2n -1的值與給定數字相同。
步驟3 - 如果找到這樣的值,則列印該數字是梅森素數。
步驟4 - 如果沒有找到這樣的值,則列印該數字不是梅森素數。
語法
在Java中,我們有內建的java.lang.Math.pow()方法來獲取任何數的冪。
以下是使用該方法獲取2的冪的語法:
power = Math.pow (inputValue,2)
多種方法
我們提供了多種方法來解決這個問題。
使用靜態輸入值
使用使用者輸入值
使用使用者自定義方法
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入值
在這種方法中,我們宣告一個變數並用一個數字初始化它。然後,使用演算法我們可以檢查該數字是否為梅森素數。
示例
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.");
}
}
}
輸出
65535 is a Mersenne number.
方法2:使用使用者輸入值
在這種方法中,我們宣告一個變數並從使用者處獲取一個數字作為值。然後,使用演算法我們可以檢查該數字是否為梅森素數。
示例
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.");
}
}
}
輸出
Enter a number: 3 3 is a Mersenne number.
方法3:使用使用者自定義方法
在這種方法中,我們宣告一個變數並用一個數字初始化它。然後,我們透過將此數字作為引數呼叫使用者自定義方法,然後在方法內部使用演算法來檢查該數字是否為梅森素數。
示例
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;
}
}
輸出
3 is a Mersenne number.
在這篇文章中,我們探討了如何使用不同的方法在Java中檢查一個數是否為梅森素數。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP