Java 中计算阶乘的方法
本文介绍了在 Java 中计算阶乘的方法和代码示例。
数 n
的阶乘是 1
和 n
之间所有自然数的乘积。在本文中,我们将看到计算数字阶乘的不同方法。
我们将首先看看如何计算小于等于 20
的数字的阶乘。这种隔离是因为 Java 的长数据类型范围有限。
20
以上数字的阶乘太大而无法放入多头的范围内。
使用 Java 中的迭代方法获取阶乘
在这个例子中,我们创建了一个 long 类型的变量 store_fact
,并使用 1
对其进行初始化。
然后我们遍历从 1
到计算阶乘的所有整数,并将循环变量值乘以 store_fact
值。我们将计算值存储到 store_fact
变量中并更新循环变量。
为了让上面的算法更清晰,我们可以这样写:
-
初始化
n
-
初始化
store_fact = 1
-
执行
for i = 1
到n
-
store_fact = store_fact*n
-
增加
i
-
返回
store_fact
在上面的算法中,store_fact
变量存储 n
的阶乘如下:
-
第一次迭代后:
store_value = 1 = 1!
-
第二次迭代后:
store_value = 1 X 2 = 2!
-
第三次迭代后:
store_value = 1 X 2 X 3 = 3!
-
第 n 次迭代后:
store_value = 1 X 2 X 3 X 4 ........ Xn = n!
现在让我们看一下上述算法的代码示例。
import java.util.Scanner;
public class SimpleTesting {
static long factCalculator(int n){
long store_fact = 1;
int i =1;
while(i <= n){
store_fact = store_fact*i;
i++;
}
return store_fact;
}
public static void main(String args[]) {
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(factCalculator(number));
}
}
输出:
Enter a number:
4
24
在 Java 中使用递归方法查找阶乘
上面的迭代方法可以转化为递归方法来求任意数的阶乘。在这种方法中,我们将基本情况视为:
if( n == 0 || n ==1){
return 1;
}
如果不满足基本条件,则返回:
n * factCalculator(n-1);
让我们看看下面的代码示例。我们使用递归方法 factCalculator()
来查找阶乘。
import java.util.*;
public class SimpleTesting {
static long factCalculator(int n){
if( n == 0 || n ==1){
return 1;
}
else{
return n * factCalculator(n-1);
}
}
public static void main(String args[]) {
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(factCalculator(number));
}
}
输出:
Enter a number:
4
24
在 Java 中使用动态方法查找阶乘
我们还可以使用动态规划方法计算数字的阶乘。此方法比其他方法更快,因为它存储较小数字的阶乘并使用这些阶乘计算较大数字的阶乘。
例如:
- 5! = 5 X 4!
- 4! = 4 X 3!
- 3! = 3 X 2!
- 2! = 2 X 1!
- 1! = 1 X 0!
- 0! = 1
在这个方法中,我们创建了一个查找表。该表存储从 0
到 20
的数字的阶乘。
我们创建直到 20
的查找表只是因为它是阶乘 long 可以存储的最大数。我们初始化了 0!
作为 1
。
然后我们使用值 0!
计算 1!
,1!
的值计算 2!
等等。看下面的代码:
import java.util.*;
public class SimpleTesting {
static long[] factCalculator(){
long[] fact_table = new long[21];
fact_table[0] = 1;
for(int i=1; i<fact_table.length; i++){
fact_table[i] = fact_table[i-1] * i;
}
return fact_table;
}
public static void main(String args[]) {
long[] table = factCalculator();
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(table[number]);
}
}
输出:
Enter a number:
5
120
在 Java 中使用 Apache Commons 查找阶乘
如果你使用 Apache Commons Math 库,请使用带有 factorial()
方法的 CombinatoricsUtils
类。它是计算任何数字的阶乘的内置方法。
该方法返回的值为 long 类型;因此,我们无法计算大于 20
的数字的阶乘。请参见下面的示例。
import java.util.Scanner;
import org.apache.commons.math3.util.CombinatoricsUtils;
public class SimpleTesting {
static long factCalculator(int n){
return CombinatoricsUtils.factorial(n);
}
public static void main(String args[]) {
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(factCalculator(number));
}
}
输出:
Enter a number:
5
120
使用 Java 8 流查找阶乘
我们还可以使用 Java 8 流 API 来计算数字的阶乘。我们将首先创建一个从 1
到 n
的数字流,其中 n
是计算其阶乘的数字。
然后我们使用 reduce 方法对元素执行归约操作。我们将 1
作为单位元素,将乘法作为关联累加函数。
看下面的代码:
import java.util.*;
import java.util.stream.LongStream;
public class SimpleTesting {
static long factCalculator(int n){
return LongStream.rangeClosed(1,n).reduce(1, (long num1, long num2) -> num1*num2);
}
public static void main(String args[]) {
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(factCalculator(number));
}
}
输出:
Enter a number:
5
120
与迭代或递归方法相比,使用 reduce()
函数有一个很大的优势。如果用于处理元素的函数是关联的,则 reduce()
操作是可并行化的。
现在,我们将计算 20
以上数字的阶乘。
在 Java 中使用 BigInteger
查找阶乘
BigInteger
类用于处理超出原始数据类型范围的非常大的数字。我们可以使用 BigInteger
来存储 20
以上数字的阶乘值。
请参见下面的示例。
import java.math.BigInteger;
import java.util.Scanner;
public class SimpleTesting {
static BigInteger factCalculator(int n){
BigInteger store_fact = BigInteger.ONE;
for (int i1 = 2; i1 <= n; i1++){
store_fact = store_fact.multiply(BigInteger.valueOf(i1));
}
return store_fact;
}
public static void main(String args[]) {
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(factCalculator(number));
scan.close();
}
}
输出:
Enter a number:
50
30414093201713378043612608166064768844377641568960512000000000000
由于我们不能使用*
运算符来乘以 BigInteger
,所以我们使用 multiply()
函数。这个方法和迭代方法一样,只是我们使用 BigInteger
而不是 long。
使用 BigIntegerMath
库查找阶乘
BigIntegerMath
库有一个内置的 factorial()
方法,可用于计算数字的阶乘。它是一个静态方法并返回一个 long 类型的值。
请参见下面的示例。
import java.util.*;
import com.google.common.math.BigIntegerMath;
public class SimpleTesting {
static long factCalculator(int n){
return BigIntegerMath.factorial(n);
}
public static void main(String args[]) {
int number;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number: ");
number = scan.nextInt();
System.out.println(factCalculator(number));
}
}
输出:
Enter a number:
50
30414093201713378043612608166064768844377641568960512000000000000
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。