1. Overview
1.概述
Given a non-negative integer n, factorial is the product of all positive integers less than or equal to n.
给定一个非负整数n,阶乘是所有小于或等于n的正整数的乘积。
In this quick tutorial, we’ll explore different ways to calculate factorial for a given number in Java.
在这个快速教程中,我们将探讨在Java中计算一个给定数字的阶乘的不同方法。
2. Factorial for Numbers up to 20
2.20以内数字的阶乘
2.1. Factorial Using a for Loop
2.1.使用for循环的因果律
Let’s see a basic factorial algorithm using a for loop:
让我们看看一个使用for循环的基本阶乘算法。
public long factorialUsingForLoop(int n) {
long fact = 1;
for (int i = 2; i <= n; i++) {
fact = fact * i;
}
return fact;
}
The above solution will work fine for numbers up to 20. But, if we try something bigger than 20, then it will fail because results would be too large to be fit into a long, causing an overflow.
上面的解决方案对于20以内的数字来说很好。但是,如果我们尝试大于20的数字,就会失败,因为结果太大,无法装入long,导致溢出。
Let’s see a few more, noting that each of these will only work for small numbers.
让我们再看几个,注意每一个这些都只对小数字有效。
2.2. Factorial Using Java 8 Streams
2.2.使用Java 8 Streams的阶乘
We can also use the Java 8 Stream API to calculate factorials quite easily:
我们还可以使用Java 8 Stream API来相当容易地计算阶乘。
public long factorialUsingStreams(int n) {
return LongStream.rangeClosed(1, n)
.reduce(1, (long x, long y) -> x * y);
}
In this program, we first use LongStream to iterate through the numbers between 1 and n. We then used reduce(), which uses an identity value and accumulator function for the reduction step.
在这个程序中,我们首先使用LongStream来迭代1到n之间的数字。然后我们使用reduce(),它使用一个身份值和累加器函数来进行还原步骤。
2.3. Factorial Using Recursion
2.3.使用递归的阶乘
And let’s see another example of a factorial program, this time using recursion:
让我们看看另一个阶乘程序的例子,这次是使用递归。
public long factorialUsingRecursion(int n) {
if (n <= 2) {
return n;
}
return n * factorialUsingRecursion(n - 1);
}
2.4. Factorial Using Apache Commons Math
2.4.使用Apache Commons Math的阶乘
Apache Commons Math has a CombinatoricsUtils class with a static factorial method that we can use to calculate the factorial.
Apache Commons Math有一个CombinatoricsUtils类,其中有一个静态factorial方法,我们可以用它来计算阶乘。
To include Apache Commons Math, we’ll add the commons-math3 dependency into our pom:
为了包括Apache Commons Math,我们将把commons-math3依赖性加入我们的pom。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
Let’s see an example using the CombinatoricsUtils class:
让我们看一个使用CombinatoricsUtils类的例子。
public long factorialUsingApacheCommons(int n) {
return CombinatoricsUtils.factorial(n);
}
Notice that its return type is long, just like our home-grown solutions.
注意它的返回类型是long,就像我们自制的解决方案。
That means here that if the computed value exceeds Long.MAX_VALUE, a MathArithmeticException is thrown.
这意味着在这里,如果计算值超过Long.MAX_VALUE,会抛出一个MathArithmeticException。
To get any bigger, we are going to need a different return type.
要想获得更大的发展,我们需要一个不同的返回类型。。
3. Factorial for Numbers Greater Than 20
3.大于20的数字的阶乘
3.1. Factorial Using BigInteger
3.1.使用BigInteger的阶乘
As discussed before, the long datatype can be used for factorials only for n <= 20.
如前所述,long数据类型只能用于n <=20的阶乘。
For larger values of n, we can use the BigInteger class from the java.math package, which can hold values up to 2^Integer.MAX_VALUE:
对于更大的n值,我们可以使用java.math包中的BigInteger类,它可以容纳高达2^Integer.MAX_VALUE的值。
public BigInteger factorialHavingLargeResult(int n) {
BigInteger result = BigInteger.ONE;
for (int i = 2; i <= n; i++)
result = result.multiply(BigInteger.valueOf(i));
return result;
}
3.2. Factorial Using Guava
3.2.使用Guava的阶乘法
Google’s Guava library also provides a utility method for calculating factorials for larger numbers.
Google的Guava库也提供了一个实用的方法,用于计算较大数字的阶乘。
To include the library, we can add its the guava dependency to our pom:
为了包含该库,我们可以将其guava依赖添加到我们的pom中。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
Now, we can use the static factorial method from the BigIntegerMath class to calculate the factorial of a given number:
现在,我们可以使用BigIntegerMath类中的静态阶乘方法来计算一个给定数字的阶乘。
public BigInteger factorialUsingGuava(int n) {
return BigIntegerMath.factorial(n);
}
4. Conclusion
4.总结
In this article, we saw a few ways of calculating factorials using core Java as well as a couple of external libraries.
在这篇文章中,我们看到了使用核心Java以及几个外部库计算阶乘的几种方法。
We first saw solutions using the long data type for calculating factorials of numbers up to 20. Then, we saw a couple of ways to use BigInteger for numbers greater than 20.
我们首先看到了使用long数据类型来计算20以内的阶乘的解决方案。然后,我们看到了一些使用BigInteger计算大于20的数字的方法。
The code presented in this article is available over on Github.
本文介绍的代码可在Github上获得over。