home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / vici_102 / examples / fib.e < prev    next >
Text File  |  1993-01-14  |  985b  |  65 lines

  1. class fib
  2. feature
  3. p , f: integer;
  4. create is
  5. do
  6.     io.putstring("Fibbonacci-Berechnung von ");
  7.     io.readint;
  8.     p := io.lastint;
  9.     io.putstring("(I)terativ oder (R)ekursiv berechnen ?");
  10.     io.readchar;
  11.     inspect io.lastchar
  12.     when 'I', 'i' then
  13.         f := fibI(p)
  14.     when 'R', 'r' then
  15.         f := fibR(p);
  16.     else
  17.         f := fib(p); -- who knows what!?
  18.     end;
  19.     
  20.     -- Ausgabe...
  21.     io.putstring("fib(");
  22.     io.putint(p);
  23.     io.putstring(") = ");
  24.     io.putint(f);
  25.     io.new_line;
  26. end;
  27.  
  28. fib(p: integer): integer is
  29. require p>=0
  30. do
  31.     result := fibI(p)
  32. end;
  33.  
  34.  
  35. fibR(p: integer): integer is
  36. -- Fibbonacci rekursiv berechnen!
  37. do
  38.     if p > 1 then
  39.         result := fibR(p-1) + fibR(p-2)
  40.     else
  41.         result := 1;
  42.     end
  43. end;
  44.  
  45. fibI(p: integer): integer is
  46. -- Fibbonacci iterativ berechnen!
  47. local f1,f2,n: integer
  48. do
  49.     from f1:=1
  50.     invariant -- Result=fib(n) and (n=0 or (f1=fib(n) and f2=fib(n-1)))
  51.     until n = p
  52.     loop
  53.         Result:=f1+f2;
  54.         f2:=f1;
  55.         f1:=Result;
  56.         n:=n+1;
  57.     end
  58. end
  59.  
  60. end
  61.  
  62.  
  63.  
  64.  
  65.