Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE fibonacci;
- FROM InOut IMPORT WriteString, WriteLn, WriteInt, ReadInt, Read;
- VAR
- proc : PROCEDURE (INTEGER, VAR BOOLEAN): INTEGER;
- result, n : INTEGER;
- ok, again, repeat, ft : BOOLEAN;
- option : CHAR;
- PROCEDURE fibonaccislow1(n: INTEGER;VAR ok: BOOLEAN): INTEGER;
- CONST
- gr = 35;
- VAR
- f: ARRAY[0..2] OF INTEGER;
- i: INTEGER;
- BEGIN
- IF (0 < n) AND (n < gr) THEN
- ok := TRUE;
- f[0] := 0;
- f[1] := 1;
- f[2] := f[0] + f[1];
- IF n <= 2 THEN
- RETURN f[n]
- ELSE
- i := 2;
- REPEAT
- f[0] := f[1];
- f[1] := f[2];
- ok := MAX(INTEGER) - f[2] > f[1];
- IF ok THEN
- f[2] := f[1] + f[0];
- i := i+ 1
- END
- UNTIL (i = n) OR NOT ok;
- IF i = n THEN
- ok := TRUE;
- RETURN f[2]
- END
- END
- ELSE ok := FALSE;
- RETURN 0
- END
- END fibonaccislow1;
- PROCEDURE fibonaccislow2(n: INTEGER;VAR ok : BOOLEAN): INTEGER;
- VAR
- f0, f1, i : INTEGER;
- BEGIN
- IF 0 <= n THEN
- ok := TRUE;
- f0 := 0;
- f1 := 1;
- i := 1;
- WHILE ok AND (i < n) DO
- ok := MAX(INTEGER) - f1 - f1 > f0;
- IF ok THEN
- f0 := f0 + f1;
- f1 := f0 + f1;
- i := i+ 2
- END
- END;
- IF ok THEN
- IF ODD(n) THEN
- RETURN f1
- ELSE
- RETURN f0
- END
- END
- ELSE
- ok := FALSE;
- RETURN 0
- END;
- END fibonaccislow2;
- PROCEDURE fibonaccirecs(n: INTEGER;VAR ok : BOOLEAN): INTEGER;
- PROCEDURE Fib(n: INTEGER): INTEGER;
- BEGIN
- IF n = 1 THEN
- RETURN n
- ELSE
- RETURN Fib(n-1) + Fib(n-2)
- END
- END Fib;
- BEGIN
- IF n >= 0 THEN
- ok := TRUE;
- RETURN Fib(n)
- ELSE
- ok := FALSE;
- RETURN 0
- END
- END fibonaccirecs;
- PROCEDURE fibonaccirecf(n: INTEGER;VAR ok: BOOLEAN): INTEGER;
- CONST
- dg = 0;
- gg = 20;
- PROCEDURE Fib(f1,f0,n: INTEGER): INTEGER;
- BEGIN
- IF n = 0 THEN
- RETURN f0
- ELSE
- RETURN Fib(f0+f1,f1,n-1)
- END
- END Fib;
- BEGIN
- IF (dg <= n) AND (gg >= n) THEN
- ok := TRUE;
- RETURN Fib(1,0,n);
- ELSE
- ok := FALSE;
- RETURN 0
- END
- END fibonaccirecf;
- PROCEDURE fibonacciff(n: INTEGER;VAR ok: BOOLEAN): INTEGER;
- VAR
- p1, p2, g1, g2, temp: INTEGER;
- chose: BOOLEAN;
- BEGIN
- IF 0 <= n THEN
- ok := TRUE;
- p1 := 0;
- p2 := 1;
- g1 := 1;
- g2 := 2;
- chose := ODD(n); (*stack overflow*)
- n := n DIV 2; (*check later why!*)
- WHILE n > 0 DO
- IF ODD(n) THEN
- temp := p1*g1;
- p1 := p1*g2 + p2*g1 - temp;
- p2 := p2*g2 + temp
- END;
- n := n DIV 2;
- IF n > 0 THEN
- temp := g1*g1;
- g1 := 2*g1*g2 - temp;
- g2 := g2*g2 + temp
- END
- END;
- IF chose THEN
- RETURN p2
- ELSE
- RETURN p1
- END
- ELSE
- ok := FALSE
- END;
- RETURN 0
- END fibonacciff;
- BEGIN
- ft := TRUE;
- REPEAT
- repeat := FALSE;
- WriteString('Enter number, n: ');
- ReadInt(n); WriteLn;
- IF ft THEN
- WriteString('Select procedure, For '); WriteLn;
- WriteString('iterative, slow.. select 1;');WriteLn;
- WriteString('iterative, fast.. select 2;');WriteLn;
- WriteString('recursive,slow.. select 3;');WriteLn;
- WriteString('recursive,fast.. select 4;');WriteLn;
- WriteString('complex, via the matrix.. select 5;');WriteLn; (*stackoverflow with this one*)
- WriteString('waiting on input..: ');
- again := FALSE;
- REPEAT
- Read(option); WriteLn;
- IF again THEN
- WriteString('Re-enter your chose, must be (nmb >=1 AND <= 5);');
- END;
- again := TRUE;
- UNTIL (option = '1') OR (option = '2') OR (option = '3') OR
- (option = '4') OR (option = '5'); WriteLn;
- CASE option OF
- '1' : proc := fibonaccislow1|
- '2' : proc := fibonaccislow2|
- '3' : proc := fibonaccirecs|
- '4' : proc := fibonaccirecf|
- '5' : proc := fibonacciff
- ELSE
- WriteString('subtle eR!')
- END;
- ft := FALSE;
- END;
- result := proc(n,ok);
- IF ok THEN
- WriteString('The Resutl is: ');
- WriteInt(result,0);
- ELSE
- WriteString('You have to re-enter number n!');
- repeat := TRUE;
- END;
- UNTIL repeat = FALSE;
- END fibonacci.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement