Mixed int/BigInt foreach interval
bearophile
bearophileHUGS at lycos.com
Wed Sep 28 10:21:20 PDT 2011
Timon Gehr:
> I mean, eg. the following is hardly useful:
>
> foreach(i;0..BigInt("10000000000000000000000000"){}
I'd like a function template like this to work even if T is BigInt:
T binomial(T)(T n, T k) {
if (k > n/2)
k = n - k;
T bc = n - k + 1;
foreach (i; 2 .. k+1)
bc = (bc * (n - k + i)) / i;
return bc;
}
Current workaround: use this instead of the foreach:
for (T i = 2; i <= k; i++)
----------------------------
Christophe:
> The copy on write behavior of BigInt makes it very poorly efficient
> to iterate on. In addition, I guess the compiler won't be able to
> optimize anything. Someone could say this is a good reason to make the
> .. syntax on BigInt harder to use.
I'd like to use BigInts often in D. So some compiler optimizations are welcome here.
Bye,
bearophile
More information about the Digitalmars-d
mailing list