SRC Modula-3 Back to the Win32 Shootout
Back to dada's perl lab

[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]  
All Source For modula3
Ackermann's Function

MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

PROCEDURE Ack(M, N: INTEGER): INTEGER =
BEGIN
    IF M = 0 THEN 
        RETURN N+1;
    ELSE
        IF N = 0 THEN
            RETURN Ack(M-1, 1);
        ELSE
            RETURN Ack(M-1, Ack(M, N-1));
        END;
    END;
END Ack;

VAR N: INTEGER;
BEGIN
    IF Params.Count > 0 THEN
        N := Scan.Int(Params.Get(1));
    ELSE
        N := 1;
    END;
    Wr.PutText (Stdio.stdout, "Ack(3," & Fmt.Int(N) & "): " & Fmt.Int(Ack(3, N)));
    Wr.Close (Stdio.stdout);
END Main.
Fibonacci Numbers

MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

PROCEDURE fib(n: INTEGER): INTEGER =
BEGIN
    IF n < 2 THEN
        RETURN 1;
    ELSE
        RETURN fib(n-2) + fib(n-1);
    END
END fib;

VAR N: INTEGER;
BEGIN
    IF Params.Count > 0 THEN
        N := Scan.Int(Params.Get(1));
    ELSE
        N := 1;
    END;
    Wr.PutText (Stdio.stdout, Fmt.Int(fib(N)));
    Wr.Close (Stdio.stdout);
END Main.
Hello World

MODULE Main;
IMPORT Wr, Stdio;
BEGIN
    Wr.PutText (Stdio.stdout, "hello world\n");
    Wr.Close (Stdio.stdout);
END Main.
Nested Loops

MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

VAR N, a, b, c, d, e, f, x: INTEGER;
BEGIN
    IF Params.Count > 0 THEN
        N := Scan.Int(Params.Get(1));
    ELSE
        N := 1;
    END;
    x := 0;
    FOR a := 1 TO N DO
        FOR b := 1 TO N DO
            FOR c := 1 TO N DO
                FOR d := 1 TO N DO
                    FOR e := 1 TO N DO
                        FOR f := 1 TO N DO
                            INC(x);
                        END;
                    END;
                END;
            END;
        END;
    END;
    Wr.PutText (Stdio.stdout, Fmt.Int(x));
    Wr.Close (Stdio.stdout);
END Main.
Random Number Generator

MODULE Main;
IMPORT Fmt, Scan, Params, Wr, Stdio;

CONST 
    IM = 139968;
    IA = 3877;
    IC = 29573;

PROCEDURE gen_random(n: REAL): REAL =
BEGIN
    last := (last * IA + IC) MOD IM;
    RETURN n * FLOAT(last) / FLOAT(IM);
END gen_random;

VAR last: INTEGER := 42;

VAR 
    n: INTEGER;
    i: INTEGER;
    result: REAL;
BEGIN
    IF Params.Count > 0 THEN
        n := Scan.Int(Params.Get(1));
    ELSE
        n := 1;
    END;
    FOR i := 1 TO n DO
        result := gen_random(100.0);
    END;
    Wr.PutText (Stdio.stdout, Fmt.Real(result, prec:=9));
    Wr.Close (Stdio.stdout);
END Main.