How to stream over a range of BigIntegers?

Panos K :

Hello I currently have this piece of code for finding factorial which is work fine

public static BigInteger factorial(BigInteger n) {
    BigInteger sum = BigInteger.ONE;
    for (BigInteger i = BigInteger.ONE; i.compareTo(n) <= 0; i = i.add(BigInteger.ONE)) {
        sum = sum.multiply(i);
    }
    return sum;
}

What I want to achieve is to convert this to a Stream<BigInteger> and write it like this

public static BigInteger factorial(BigInteger n) {
    return getBigIntegerStream(n).reduce(BigInteger.ONE, BigInteger::multiply);
}

So my question is how I can get a Stream<BigInteger> similar to how I can declare an IntStream?

IntStream.range(1, myInt);
Eran :

Perhaps something like this:

public static BigInteger factorial(BigInteger n) {
    return Stream.iterate (BigInteger.ONE, i -> i.add(BigInteger.ONE)).limit(Integer.parseInt(n.toString())).reduce(BigInteger.ONE, BigInteger::multiply);
}

EDIT: I forgot to limit the stream. Fixed now.

Of course it would be simpler to just accept an int (or a long) as the argument:

public static BigInteger factorial(int n) {
    return Stream.iterate (BigInteger.ONE, i -> i.add(BigInteger.ONE)).limit(n).reduce(BigInteger.ONE, BigInteger::multiply);
}

It is very unlikely you will even need to calculate the factorial of a number larger than Integer.MAX_VALUE. The factorial of such a number would be huge, and would probably take very long to calculate.

EDIT: Not a proper benchmark, but factorial(100000) took me 5 seconds, and factorial(1000000) took 8 minutes. At this rate, factorial(Long.MAX_VALUE) or even factorial(Integer.MAX_VAULE) will take very very long time. Therefore I don't see the point of requiring a BigInteger argument.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=122842&siteId=1