2023-01-04 22:32:55 +00:00
|
|
|
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-04 22:32:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 16:44:30 +00:00
|
|
|
fib_iter(n) {
|
2023-01-04 22:32:55 +00:00
|
|
|
x0 = 1;
|
|
|
|
x1 = 1;
|
|
|
|
i = 0;
|
|
|
|
while i < n {
|
|
|
|
t = x0 + x1;
|
|
|
|
x0 = x1;
|
|
|
|
x1 = t;
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
return x0;
|
|
|
|
}
|