Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
27
Task/Factorial/Java/factorial-4.java
Normal file
27
Task/Factorial/Java/factorial-4.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import java.math.BigInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class Factorial {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Valid only for integer arguments 1, 2, ... , 20
|
||||
Function<Integer, Long> factorialPositive = n -> LongStream.rangeClosed(2, n).reduce(1, (a, b) -> a * b);
|
||||
|
||||
// Valid for integer arguments <= 20
|
||||
Function<Integer, Long> factorial = n -> {
|
||||
if ( n < 0 || n > 20 ) {
|
||||
throw new AssertionError("Argument is out of range: " + n);
|
||||
}
|
||||
|
||||
return factorialPositive.apply(n);
|
||||
};
|
||||
|
||||
// Return a BigInteger value
|
||||
Function<Integer, BigInteger> factorialBig = n -> Stream.iterate(BigInteger.ONE, i -> i.add(BigInteger.ONE))
|
||||
.limit(n)
|
||||
.reduce(BigInteger.ONE, BigInteger::multiply);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue