rhai/scripts/fibonacci.rhai

26 lines
507 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 = 30;
fn fib(n) {
if n < 2 {
n
} else {
fib(n-1) + fib(n-2)
}
}
print("Ready... Go!");
2020-05-13 05:56:48 +02:00
let now = timestamp();
2020-04-13 17:38:10 +02:00
let result = fib(target);
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);
2020-05-13 05:56:48 +02:00
if result != 832_040 {
print("The answer is WRONG! Should be 832,040!");
}