RosettaCodeData/Task/Compile-time-calculation/D/compile-time-calculation-1.d

16 lines
306 B
D
Raw Permalink Normal View History

2015-02-20 00:35:01 -05:00
long fact(in long x) pure nothrow @nogc {
2013-04-10 16:57:12 -07:00
long result = 1;
2015-02-20 00:35:01 -05:00
foreach (immutable i; 2 .. x + 1)
2013-04-10 16:57:12 -07:00
result *= i;
return result;
}
void main() {
// enum means "compile-time constant", it forces CTFE.
enum fact10 = fact(10);
2015-02-20 00:35:01 -05:00
import core.stdc.stdio;
2013-04-10 16:57:12 -07:00
printf("%ld\n", fact10);
}