Powtórka z prehistorii, ku pamięci: liczby Fibonacciego w Pascalu. Autor: Maciej Czekaj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
uses Math, crt, SysUtils;
var
i:integer;

function fib_iter(liczba:Longword):Longword;
var a:Longword; b:Longword; c:Longword; i:Longword;
begin
  a := 1;
  b := 1;
  if liczba <= 2 then
    fib_iter:=1
  else
    for i := 3 to liczba do
    begin
      c := a+b;
      a := b;
      b := c;
    end;
    fib_iter:=c;
end;

function fib_lin(liczba:Longword):Longword;
begin
  fib_lin:=Longword(Round((1/sqrt(5))*(power((1+sqrt(5)/2),liczba))-(1/sqrt(5))*(power((1-sqrt(5)/2),liczba))));
end;

function fib_rek(liczba:Longword):Longword;
begin
     if liczba=0 then fib_rek:=0
        else if liczba<=2 then fib_rek:=1
             else fib_rek:=fib_rek(liczba-1)+fib_rek(liczba-2);
end;

begin
   for i:=1 to 200 do writeln(fib_iter(i));
   for i:=1 to 200 do writeln(fib_lin(i));
   for i:=1 to 20 do writeln(fib_rek(i));
end.