使用Java 8 IntStream计算阶乘? [英] calculating factorial using Java 8 IntStream?
问题描述
我在Java 8和lambda表达式以及Stream
中相对较新,我可以使用for
循环或递归来计算阶乘.但是,有没有一种方法可以使用IntStream
来计算数字的阶乘?我可以使用整数范围内的阶乘.
I am relatively new in Java 8 and lambda expressions as well as Stream
, i can calculate factorial using for
loop or recursion. But is there a way to use IntStream
to calculate factorial of a number ? I am fine even with factorial in integer range.
我在此处阅读了IntStream
文档, http://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html ,我可以看到很多方法,但是不确定我可以使用哪种方法来计算阶乘
I read through IntStream
docs here, http://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html and i can see so many methods but not sure which one I can use to calculate factorial.
例如,有rang
方法说,
range(int startInclusive,int endExclusive)返回一个顺序 从startInclusive(含)到endExclusive的有序IntStream (不包括)以1为增量.
range(int startInclusive, int endExclusive) Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.
所以我可以用它为IntStream提供要乘以计算阶乘的数字范围.
so I can use it to provide the range of numbers to IntStream to be multiplied to calculate factorial.
number = 5;
IntStream.range(1, number)
但是如何将这些数字相乘以获得阶乘?
but how to multiply those numbers to get the factorial ?
推荐答案
您可以使用 查看全文