Recursive with functional Java computation factorials… here is a solution to the problem.
Recursive with functional Java computation factorials
I tried to calculate the factorial in a functional way.
Here’s what I do:
private static Function<BigInteger, BigInteger> factorial = x -> BigInteger.ONE.equals(x)
? BigInteger.ONE
: x.multiply(Main.factorial.apply(x.subtract(BigInteger.ONE)));
I got StackOverflowError
! while trying to get 11111
But when I calculate the factorial using this method:
private static BigInteger factorial(BigInteger request) {
if (BigInteger.ONE.equals(request)) return BigInteger.ONE;
else return request.multiply(factorial(request.subtract(BigInteger.ONE)));
}
I can get results without StackOverflowError
.
Does the functional style work poorly? Why?