home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / dada / sample.dad < prev    next >
Text File  |  1986-02-27  |  896b  |  49 lines

  1.  
  2.  
  3.  
  4.  
  5.     { A sample Dada program. Calculates and prints the sequence
  6.       of "hailstone numbers" for a given input. For each term N
  7.       in this series the calculation is done as follows: If N is
  8.       even, the next term is N/2; if N is odd, the next term is
  9.       3N+1. The calculation ends when N is 1. (Whether or not the
  10.       program halts on all inputs is an interesting question.)      }
  11.  
  12.  
  13. program Hailstone;
  14.  
  15. var
  16.  N   : integer;
  17.  Odd : boolean;
  18.  
  19. procedure NextTerm;
  20.  
  21.  procedure CheckOdd;
  22.   begin
  23.    if (N mod 2 = 0) then Odd := False else Odd := True
  24.   end;
  25.  
  26.  procedure DownStep;
  27.   begin
  28.    N := N/2
  29.   end;
  30.  
  31.  procedure UpStep;
  32.   begin
  33.    N := 3*N+1
  34.   end;
  35.  
  36.  begin   { NextTerm }
  37.   CheckOdd;
  38.   if Odd then UpStep else DownStep
  39.  end;
  40.  
  41. begin    { main program }
  42.  ReadLn N;
  43.  while N > 1 do
  44.   begin
  45.    WriteLn N;
  46.    NextTerm
  47.   end;
  48. end.
  49.