xlang/test/fib.x

29 lines
361 B
Plaintext
Raw Normal View History

2023-01-07 07:54:53 +00:00
main() : int {
i := 0;
2023-01-05 16:44:30 +00:00
while i < 10 {
print fib_rec(i);
print fib_iter(i);
i = i + 1;
}
}
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;
i := 0;
while i < n {
2023-01-07 07:54:53 +00:00
t := x0 + x1;
x0 = x1;
x1 = t;
i = i + 1;
}
return x0;
}