xlang/test/fib.x

29 lines
326 B
Plaintext
Raw Normal View History

main() {
2023-01-05 16:44:30 +00:00
i = 0;
while i < 10 {
print fib_rec(i);
print fib_iter(i);
i = i + 1;
}
}
fib_rec(n) {
if n < 2 {
return 1;
}
return fib_rec(n - 1) + fib_rec(n - 2);
}
2023-01-05 16:44:30 +00:00
fib_iter(n) {
x0 = 1;
x1 = 1;
i = 0;
while i < n {
t = x0 + x1;
x0 = x1;
x1 = t;
i = i + 1;
}
return x0;
}