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中检查一个数字是否是特殊数字。