home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / elan / demo / nfac.eln < prev    next >
Text File  |  1987-08-21  |  1KB  |  69 lines

  1.  
  2. factorials:
  3.   explain;
  4.   make room for 50 digits;
  5.   start at one;
  6.   print all factorials;
  7.   put ("sorry, space exhausted");
  8.   line (1).
  9.  
  10.   explain:
  11.     put ("This program computes n factorial in at most 50 digits.");
  12.     line (1);
  13.     put ("It shows how run time errors lead to a traceback.");
  14.     line (2);
  15.     put ("          n     digits   n! ");
  16.     line (2).
  17.   
  18.   make room for 50 digits:
  19.     ROW 50 INT VAR nfac.
  20.   
  21.   start at one:
  22.     INT VAR n :: 1;
  23.     nfac [1] := 1;
  24.     INT VAR m :: 1.
  25.   
  26.   print all factorials:
  27.     REP
  28.       compute next factorial;
  29.       print it
  30.     UNTIL false
  31.     ENDREP.
  32.   
  33.     compute next factorial:
  34.       n INCR 1;
  35.       INT VAR carry :: 0;
  36.       INT VAR i;
  37.       FOR i FROM 1 UPTO m
  38.       REP compute ith position
  39.       ENDREP;
  40.       process carry.
  41.     
  42.       compute ith position:
  43.         INT VAR figure :: nfac [i] * n + carry;
  44.         carry := figure DIV 10;
  45.         nfac [i] := figure - 10 * carry.
  46.       
  47.       process carry:
  48.         WHILE carry > 0
  49.         REP
  50.           figure := carry;
  51.           carry := figure DIV 10;
  52.           m INCR 1;
  53.           nfac [m] := figure - 10 * carry
  54.         ENDREP.
  55.       
  56.     print it:
  57.       put (n);
  58.       put (m);
  59.       put ("   ");
  60.       INT VAR j;
  61.       FOR j FROM m DOWNTO 1
  62.       REP put (jth digit)
  63.       ENDREP;
  64.       line (1).
  65.     
  66.       jth digit:
  67.         "0123456789" SUB nfac [j] + 1.
  68.       
  69.