使用Java 8 IntStream计算阶乘? [英] calculating factorial using Java 8 IntStream?

查看:111
本文介绍了使用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 ?

推荐答案

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆