home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / LANGUAGS / MODULA2 / FRACTION.MOD < prev    next >
Text File  |  2000-06-30  |  872b  |  32 lines

  1. (* Compute a table of exact fractions 1/2, 1/3. ... , 1/64.
  2.    If  the fraction has a period, print an apostrophe in front
  3.    of its first digit and truncate after its last digit. *)
  4.  
  5. MODULE fractions;
  6. FROM InOut IMPORT Write, WriteLn, WriteString, WriteCard;
  7. (* fractions to the base b *)
  8.  
  9. CONST Base = 10;
  10.       N = 32;
  11.  
  12. VAR i,j,m,rem: CARDINAL;
  13.     d: ARRAY [1..N] OF CARDINAL; (* digits *)
  14.     x: ARRAY [0..N] OF CARDINAL; (* index  *)
  15. BEGIN
  16.   FOR i := 2 TO N DO
  17.     FOR j := 0 TO i-1 DO x[j] := 0 END;
  18.     m := 0; rem := 1;
  19.     REPEAT
  20.       m:= m+1; x[rem] := m;
  21.       rem := Base*rem;
  22.       d[m] := rem DIV i;
  23.       rem := rem MOD i;
  24.     UNTIL x[rem] # 0;
  25.     WriteCard(i,6); WriteString(' 0.');
  26.     FOR j := 1 TO x[rem]-1 DO Write(CHR(d[j]+ORD('0'))) END;
  27.     Write("'");
  28.     FOR j := x[rem] TO m DO Write(CHR(d[j]+ORD('0'))) END;
  29.     WriteLn;
  30.   END
  31. END fractions.
  32.