如何在Java中檢查一個數是否為強數?
如果一個數的每位數字的階乘之和等於該數本身,則稱該數為強數。
更清晰地解釋,我們需要找到給定數字每位數字的階乘。然後計算這些階乘的和。最後,我們將和與輸入數字進行比較,如果它們相等,則給定數字是強數,否則不是。
在本文中,我們將學習如何使用Java程式語言來檢查一個數是否為強數。
一些示例
示例1
輸入數字是145。
讓我們使用強數的邏輯來檢查它。
The factorial of 1, 4 and 5 is 1, 24 and 120. The sum of these factorials = 1 + 24 + 120 = 145
正如我們在這裡看到的,階乘的和與輸入值相同。
因此,145是一個強數。
示例2
輸入數字是534。
讓我們使用強數的邏輯來檢查它。
The factorial of 5, 3 and 4 is 120, 6 and 24. The sum of these factorials = 120 + 6 + 24 = 150
正如我們在這裡看到的,階乘的和與輸入值不同。
因此,534不是一個強數。
演算法
演算法1
步驟1 − 獲取一個整數,可以透過初始化或使用者輸入獲得。
步驟2 − 使用模運算子 (%) 依次提取每一位數字,同時使用while迴圈找到每位數字的階乘,並跟蹤階乘的和。
步驟3 − 最後將和與輸入數字進行比較
步驟4 − 如果和與輸入值相等,則列印結果,表明給定數字是強數;否則,該數字不是強數。
演算法2
步驟1 − 獲取一個整數,可以透過初始化或使用者輸入獲得。
步驟2 − 然後建立一個數組,並儲存相應索引位置的階乘。(在索引 0 和索引 1 處儲存值 1),索引 2 將儲存 2 的階乘,索引 3 將儲存 3 的階乘……最後一個索引 9 將儲存 9 的階乘。
步驟3 − 使用模運算子 (%) 依次提取每一位數字,並根據數字從陣列中找到其階乘,並跟蹤階乘的和。
步驟4 − 最後將和與輸入數字進行比較
步驟5 − 如果和與輸入值相等,則列印結果,表明給定數字是強數;否則,該數字不是強數。
多種方法
我們提供了三種不同的方法。
使用靜態輸入值
使用使用者自定義方法和陣列
讓我們依次檢視程式及其輸出。
方法1:使用靜態輸入值
在這種方法中,在程式中將數字作為靜態輸入,然後使用演算法 1,我們可以檢查該數字是否為強數。
示例
import java.util.*; public class Main { //main method public static void main(String[] args) { //declare an int variable and initialize a number as value int inputNumber = 145; //declare a variable for iteration int i; //declare variables for factorial value and the extracted digits int factorial,digit; //declare a variable to store the sum value int sum = 0; //transfer the input value to a temporary variable int temp = inputNumber; //start looping for calculating the result while(temp != 0) { i = 1; factorial = 1; //extracting the digit digit = temp % 10; //get the factorial of the digit while(i <= digit) { factorial = factorial * i; i++; } //store the sum value sum = sum + factorial; //removing the digit one by one temp = temp / 10; } //check condition if(sum == inputNumber) //if sum value is equal to input number System.out.println(inputNumber + " is a strong number\n"); else //if sum value is not equal to input number System.out.println(inputNumber + " is not a strong number\n"); } }
輸出
145 is a strong number
方法2:使用使用者自定義方法和陣列
在這種方法中,將靜態數字作為輸入,並將此數字作為引數傳遞給使用者自定義方法,然後在方法內部使用演算法 2,我們可以檢查該數字是否為強數。
示例
public class Main { //main method public static void main (String[] args) { //declare an int variable and initialize it with a number int inp = 2; //in if condition call the user defined function //by passing the input value to the method as parameter if(checkStrong(inp)) { //if true then it is a strong number System.out.println(inp + " is a strong numbrer."); } else { //if false then it is not a strong number System.out.println(inp + " is not a strong number."); } } //user defined method to check strong number static boolean checkStrong(int inputNumber) { //declare an array to store all the factorial value from 0 to 9 int factorial[] = new int[10]; //store 1 in 0th and 1st index of factorial //this is just to store each digits factorials at its respective index position //like the 1st index will hold factorial of 1, 2nd index for factorial of 2, 3rd index for factorial of 3... factorial[0] = factorial[1] = 1; //initiating the loop to find the factorials for (int i = 2; i<10; ++i) factorial[i] = factorial[i-1] * i; //declare an int variable 'sum' to store the sum value int sum = 0; //declare a temporary variable to store the input number int temp = inputNumber; //initiate the iteration for finding the sum of the factorials of the digits while (temp>0) { //get the factorial of the digit from the array sum += factorial[temp%10]; //removing the digit after calculation temp /= 10; } //if the sum value is equal to input number return true return (sum == inputNumber); } }
輸出
2 is a strong number.
在本文中,我們探討了如何使用不同的方法在Java中檢查一個數是否為強數。