如何在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 special number\n"); else //if sum value is not equal to input number System.out.println(inputNumber + " is not a special number\n"); } }

輸出

145 is a special 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(checkSpecial(inp)) { //if true then it is a special number System.out.println(inp + " is a special number."); } else { //if false then it is not a special number System.out.println(inp + " is not a special number."); } } //user defined method to check special number static boolean checkSpecial(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 special number.

在本文中,我們探討了如何使用不同的方法在Java中檢查一個數是否為特殊數。

更新於:2022年11月17日

5000+ 瀏覽量

啟動你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.