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 - 如果阶乘之和和输入值相等,那么我们可以打印结果为给定的数字是一个强数,否则该数字不是一个强数。
多种方法
我们提供了3种不同的方法来解决问题。
- 通过使用静态输入值
-
通过使用用户定义的方法和数组
让我们逐个查看程序及其输出。
方法-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中检查一个数字是否是强数。