japl/examples/factorial.jpl

20 lines
458 B
Plaintext
Raw Permalink Normal View History

2020-07-29 16:14:17 +02:00
fun factorial(n) {
// Computes the factorial of n iteratively
if (n < 0) return nil;
if (n == 1 or n == 2) return n;
var fact = 1;
for (var i = 1; i < n + 1; i = i + 1) {
fact = fact * i;
}
return fact;
}
var start = clock();
2020-10-22 18:04:27 +02:00
print("Computing factorials from 0 to 200");
2020-07-29 16:14:17 +02:00
for (var i = 0; i < 201; i = i + 1) factorial(i);
var result = clock() - start;
2020-10-22 18:04:27 +02:00
print("Computed factorials in " + stringify(result) + " seconds");
2020-07-29 16:14:17 +02:00