home *** CD-ROM | disk | FTP | other *** search
-
- factorials:
- explain;
- make room for 50 digits;
- start at one;
- print all factorials;
- put ("sorry, space exhausted");
- line (1).
-
- explain:
- put ("This program computes n factorial in at most 50 digits.");
- line (1);
- put ("It shows how run time errors lead to a traceback.");
- line (2);
- put (" n digits n! ");
- line (2).
-
- make room for 50 digits:
- ROW 50 INT VAR nfac.
-
- start at one:
- INT VAR n :: 1;
- nfac [1] := 1;
- INT VAR m :: 1.
-
- print all factorials:
- REP
- compute next factorial;
- print it
- UNTIL false
- ENDREP.
-
- compute next factorial:
- n INCR 1;
- INT VAR carry :: 0;
- INT VAR i;
- FOR i FROM 1 UPTO m
- REP compute ith position
- ENDREP;
- process carry.
-
- compute ith position:
- INT VAR figure :: nfac [i] * n + carry;
- carry := figure DIV 10;
- nfac [i] := figure - 10 * carry.
-
- process carry:
- WHILE carry > 0
- REP
- figure := carry;
- carry := figure DIV 10;
- m INCR 1;
- nfac [m] := figure - 10 * carry
- ENDREP.
-
- print it:
- put (n);
- put (m);
- put (" ");
- INT VAR j;
- FOR j FROM m DOWNTO 1
- REP put (jth digit)
- ENDREP;
- line (1).
-
- jth digit:
- "0123456789" SUB nfac [j] + 1.
-