On 05/09/2015 07:47 AM, Russel Winder via Digitalmars-d-learn wrote:

> Of course none of the implementation can calculate factorial(24) as
> they are using hardware values which are bounded and cannot store
> reasonable numbers.
>
> Could use iota. Oh no we can't as BigNums are not integral.

I don't have experience with BigInt but the following worked:

import std.stdio;
import std.bigint;
import std.range;
import std.algorithm;

struct BigIntRange
{
    BigInt front;

    enum empty = false;

    void popFront()
    {
        ++front;
    }
}

BigIntRange bigInts(long first = 0)
{
    return BigIntRange(BigInt(first));
}

BigInt factorial(size_t n)
{
    return bigInts(1).take(n).reduce!((a, b) => a *= b);
}

void main()
{
    writeln(factorial(1000));    // prints many digits
}

Ali

Reply via email to