24 lines
489 B
Plaintext
24 lines
489 B
Plaintext
func Fib1(N); \Return Nth Fibonacci number using iteration
|
|
int N;
|
|
int Fn, F0, F1;
|
|
[F0:= 0; F1:= 1; Fn:= N;
|
|
while N > 1 do
|
|
[Fn:= F0 + F1;
|
|
F0:= F1;
|
|
F1:= Fn;
|
|
N:= N-1;
|
|
];
|
|
return Fn;
|
|
];
|
|
|
|
func Fib2(N); \Return Nth Fibonacci number using recursion
|
|
int N;
|
|
return if N < 2 then N else Fib2(N-1) + Fib2(N-2);
|
|
|
|
int N;
|
|
[for N:= 0 to 20 do [IntOut(0, Fib1(N)); ChOut(0, ^ )];
|
|
CrLf(0);
|
|
for N:= 0 to 20 do [IntOut(0, Fib2(N)); ChOut(0, ^ )];
|
|
CrLf(0);
|
|
]
|