如何在Java中檢查一個數是否為指標素數?
如果一個素數的各位數字乘積與該素數本身的和是下一個素數,則稱該數為指標素數。
為了更清楚地說明,我們取一個素數,將它的各位數字相乘,並將乘積與原素數相加。如果結果是原素數的下一個素數,那麼這個數就是一個指標素數。
一些指標素數的例子:23, 61, 1123, 1231 ...等等。
在本文中,我們將學習如何使用Java程式語言來檢查一個數是否為指標素數。
舉幾個例子
例1
輸入數字為23。
讓我們使用指標素數的邏輯來檢查它。
23的各位數字乘積 = 2 * 3 = 6。
將該值與原數相加 = 23 + 6 = 29。
原數的下一個素數是29。
我們注意到計算出的數字和下一個素數相同。
因此,23是一個指標素數。
例2
輸入數字為1123。
讓我們使用指標素數的邏輯來檢查它。
1123的各位數字乘積 = 1 * 1 * 2 * 3 = 6。
將該值與原數相加 = 1123 + 6 = 1129。
原數的下一個素數是1129。
我們注意到計算出的數字和下一個素數相同。
因此,1123是一個指標素數。
例3
輸入數字為147。
讓我們使用指標素數的邏輯來檢查它。
147的各位數字乘積 = 1 * 4 * 7 = 28。
將該值與原數相加 = 147 + 28 = 175。
我們注意到計算出的數字不是素數。
因此,147不是一個指標素數。
演算法
步驟1 - 獲取輸入數字,可以透過初始化或使用者輸入。
步驟2 - 檢查輸入數字是否為素數。
步驟3 - 確定原數的下一個素數。
步驟4 - 使用計算指標素數的演算法。
步驟5 - 如果計算出的值和下一個素數相同,則輸入數字為指標素數,否則不是。
多種方法
我們提供了不同的方法來解決這個問題。
使用靜態輸入值和使用者自定義方法
使用使用者輸入值和使用者自定義方法
讓我們逐一檢視程式及其輸出。
方法1:使用靜態輸入值和使用者自定義方法
在這種方法中,我們宣告一個具有靜態輸入的變數,然後使用演算法來檢查該數字是否為指標素數。
示例
public class Main {
public static void main(String[] args) {
// Declare a variable and store the value by static input method
int inputNumber = 23;
// call the function to check the pointer prime number
if (checkPointerPrime(inputNumber))
System.out.print(inputNumber + " is a pointer prime number.");
else
System.out.print(inputNumber + " is not a pointer prime number.");
}
//user-defined method to calculate product value of digits
static int digitProduct(int num) {
int prod = 1;
//initiate loop to calculate product value
while (num != 0) {
prod = prod * (num % 10);
//remove the last digit
num = num / 10;
}
return prod;
}
// user-defined method to check the prime number
public static boolean checkPrim(int num) {
if (num <= 1)
return false;
//initiate the loop
for (int i = 2; i < num; i++)
//if condition to check whether the number is divisible by any number or not
if (num % i == 0)
//if true then return false
return false;
//otherwise return true
return true;
}
// user-defined function to check the number is pointer prime number or not
static int nextPrimeNum(int num) {
//starting phase
if (num <= 1)
return 2;
int nextPrime = num;
boolean flag = false;
// loop to check continuously for prime number
while (!flag) {
nextPrime++;
if (checkPrim(nextPrime))
flag = true;
}
return nextPrime;
}
// user-defined method to check Pointer-Prime numbers
static boolean checkPointerPrime(int num) {
//condition for pointer prime number
if (checkPrim(num) &&
(num + digitProduct(num) == nextPrimeNum(num)))
return true;
else
return false;
}
}
輸出
23 is a pointer prime number
方法2:使用使用者輸入值和使用者自定義方法
在這種方法中,我們要求使用者輸入一個數字,並將此數字作為引數傳遞給使用者自定義方法。然後,在方法內部,使用演算法來檢查該數字是否為指標素數。
示例
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//create object of Scanner class
Scanner sc=new Scanner(System.in);
//ask user to give the input
System.out.print("Enter a number: ");
//declare a variable and store the input value
int inputNumber=sc.nextInt();
// call the function to check the pointer prime number
if (checkPointerPrime(inputNumber))
System.out.print(inputNumber + " is a pointer prime number.");
else
System.out.print(inputNumber + " is not a pointer prime number.");
}
//user-defined method to calculate product value of digits
static int digitProduct(int num) {
int prod = 1;
//initiate loop to calculate product value
while (num != 0) {
prod = prod * (num % 10);
//remove the last digit
num = num / 10;
}
return prod;
}
// user-defined method to check the prime number
public static boolean checkPrim(int num) {
if (num <= 1)
return false;
//initiate the loop
for (int i = 2; i < num; i++)
//if condition to check whether the number is divisible by any number or not
if (num % i == 0)
//if true then return false
return false;
//otherwise return true
return true;
}
// user-defined function to check the number is pointer prime number or not
static int nextPrimeNum(int num) {
//starting phase
if (num <= 1)
return 2;
int nextPrime = num;
boolean flag = false;
// loop to check continuously for prime number
while (!flag) {
nextPrime++;
if (checkPrim(nextPrime))
flag = true;
}
return nextPrime;
}
// user-defined method to check Pointer-Prime numbers
static boolean checkPointerPrime(int num) {
//condition for pointer prime number
if (checkPrim(num) &&
(num + digitProduct(num) == nextPrimeNum(num)))
return true;
else
return false;
}
}
輸出
Enter a number: 1123 1123 is a pointer prime number
在本文中,我們探討了如何使用不同的方法在Java中檢查一個數是否為指標素數。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP