home *** CD-ROM | disk | FTP | other *** search
- class fib
- feature
- p , f: integer;
- create is
- do
- io.putstring("Fibbonacci-Berechnung von ");
- io.readint;
- p := io.lastint;
- io.putstring("(I)terativ oder (R)ekursiv berechnen ?");
- io.readchar;
- inspect io.lastchar
- when 'I', 'i' then
- f := fibI(p)
- when 'R', 'r' then
- f := fibR(p);
- else
- f := fib(p); -- who knows what!?
- end;
-
- -- Ausgabe...
- io.putstring("fib(");
- io.putint(p);
- io.putstring(") = ");
- io.putint(f);
- io.new_line;
- end;
-
- fib(p: integer): integer is
- require p>=0
- do
- result := fibI(p)
- end;
-
-
- fibR(p: integer): integer is
- -- Fibbonacci rekursiv berechnen!
- do
- if p > 1 then
- result := fibR(p-1) + fibR(p-2)
- else
- result := 1;
- end
- end;
-
- fibI(p: integer): integer is
- -- Fibbonacci iterativ berechnen!
- local f1,f2,n: integer
- do
- from f1:=1
- invariant -- Result=fib(n) and (n=0 or (f1=fib(n) and f2=fib(n-1)))
- until n = p
- loop
- Result:=f1+f2;
- f2:=f1;
- f1:=Result;
- n:=n+1;
- end
- end
-
- end
-
-
-
-
-