rhai/scripts/fibonacci.rhai

31 lines
587 B
Plaintext
Raw Normal View History

2020-04-13 17:38:10 +02:00
// This script calculates the n-th Fibonacci number using a really dumb algorithm
// to test the speed of the scripting engine.
const target = 28;
2020-04-13 17:38:10 +02:00
fn fib(n) {
if n < 2 {
n
} else {
fib(n-1) + fib(n-2)
}
}
print("Running Fibonacci(28) x 5 times...");
2020-04-13 17:38:10 +02:00
print("Ready... Go!");
let result;
2020-05-13 05:56:48 +02:00
let now = timestamp();
for n in range(0, 5) {
result = fib(target);
}
2020-04-13 17:38:10 +02:00
print("Finished. Run time = " + now.elapsed + " seconds.");
2020-05-13 05:56:48 +02:00
2020-04-13 17:38:10 +02:00
print("Fibonacci number #" + target + " = " + result);
if result != 317_811 {
print("The answer is WRONG! Should be 317,811!");
}