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 / SUM10000.MOD < prev    next >
Text File  |  2000-06-30  |  959b  |  42 lines

  1. (* 1. Compute the sum 1 - 1/2 + 1/3 - 1/4 + ... - 1/10000 four ways:
  2.      A. proceed strictly from left to right,
  3.      B. sum positive and negative terms separately,
  4.      C. proceed strictly from fight to left,
  5.      D. as in 2, but from right to left.
  6.    Explain the differences in the results. *)
  7.  
  8. MODULE sum10000;
  9.  
  10. FROM InOut     IMPORT WriteLn;
  11. FROM RealInOut IMPORT WriteReal;
  12.  
  13. CONST n = 10000;
  14.  
  15. VAR i : CARDINAL;
  16.     x, y, s1, s2p, s2n : REAL;
  17.  
  18. BEGIN
  19.   i := 1;  s1 := 0.0;  s2p := 0.0;  
  20.   s2n := 0.0;
  21.   REPEAT
  22.     x := 1.0/FLOAT(i);   y := 1.0/FLOAT(i+1);
  23.     s1 := s1 + x - y;
  24.     s2p := s2p + x;  s2n := s2n + y;
  25.     i := i + 2;
  26.   UNTIL i > n;
  27.   WriteReal(s1,12);
  28.   WriteReal(s2p-s2n,12);
  29.   i := n;
  30.   s1 := 0.0;  s2p := 0.0;  s2n := 0.0;
  31.   REPEAT
  32.     x := 1.0/FLOAT(i-1);  y := 1.0/FLOAT(i);
  33.     s1 := s1 + x - y;
  34.     s2p := s2p + x;  s2n := s2n + y;
  35.     i := i-2;
  36.   UNTIL i = 0;
  37.   WriteReal(s1,12);
  38.   WriteReal(s2p-s2n,12);
  39.   WriteLn;
  40. END sum10000.
  41.  
  42.