MODULE fact3; (*--------------------------------------------------------- File name: fact3.pm Author: AG, MM Date: 5/19/05 Problem: This program computes factorial for the first N items on the defined chain. Example of Execution: Enter N?: 10 N! = 1 2 6 24 120 720 5040 40320 362880 3628800 Cycles: N shifts + N multiplications. *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR LFact, LN: chain OF INTEGER; K, RightLimit: INTEGER; (*---------------------------------------------------------- MAIN ----------------------------------------------------------*) BEGIN (*reading*) WriteString("Enter N?: "); ReadInt(RightLimit); (* Read N *) (*initializing data *) LN:= ID(chain); (* LN = 1 2 3 4 5 6.. *) LFact:= LN; IF LN <= RightLimit THEN FOR K:=1 TO RightLimit DO LN:= MOVE.right(LN); (* Shift LN and multiply it by LFact *) LFact:=LFact*LN; END; (*WHILE*) (*printing*) WriteString(" N! = " ); WriteInt(LFact, 2); (*Only prints the activated cells *) END; (*IF *) END fact3.