xlang/test/fib.x

25 lines
339 B
Plaintext
Raw Normal View History

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