whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

36 Visitors Online


 
...calculate a fibonacci number?
Autor: Dev4u.ch
Homepage: http://www.dev4u.ch
[ Print tip ]  

Tip Rating (17):  
     


{
  Fibonacci integers are defined as:
  Fibonacci Zahlen sind wie folgt definiert:

  fib[n+2] = fib[n+1] + fib[n];
  fib[1] = 1;
  fib[0] = 1;

  Example/Beispiel: fib[4] = fib[3] + fib[2] = fib[2] + fib[1] + fib[1] + fib[0] =
                    fib[1] + fib[0] + fib[1] + fib[1] + fib[0] = 5
}

function  fibit(n: Integer): Integer;
var
  
a, b, i, temp: Integer;
begin
  
temp := 1;
  a := 1;
  b := 1;
  for i := 1 to n - 1 do
  begin
    
temp := a + b;
    a := b;
    b := temp;
  end;
  Result := temp;
end;

function fibrec(n: Integer): Integer;
var
  
temp: Integer;
begin
  
temp := 0;
  if (n = 0) then temp := 1;
  if (n = 1) then temp := 1;
  if (n > 1) then temp := fibrec(n - 1) + fibrec(n - 2);
  Result := temp;
end;


// Example:
procedure TForm1.Button1Click(Sender: TObject);
begin
  
ShowMessage(IntToStr(fibit(10)));
  ShowMessage(IntToStr(fibrec(10)));
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners