The Ada Program: triples.adb

  1 -- triples.adb -- generate and print pythagorean triples
  2 
  3 -- A pythagorean triple (A,B,C) has the property that A**2+B**2 = C**2.
  4 -- For any N, the triple (2*N+1,2*N+2*N**2,2*N+2*N**2+1) is pythagorean.
  5 
  6 with
  7   Ada.Command_Line,     -- access to external execution env (Ada95 A.15)
  8   Ada.Text_IO,          -- usual text (Latin 1) input/output (A.10.1)
  9   Ada.Integer_Text_IO;  -- preinstantiated generic for Integer IO
 10 use Ada;
 11 
 12 procedure Triples is
 13 
 14    Arg: constant String := Command_Line.Argument (1);
 15    N  : Integer;        -- (from the user) number of triples wanted
 16    L  : Positive;       -- last postion in string (ignored)
 17    Two_I, Sum: Integer; -- intermediate values
 18 
 19 begin
 20 
 21    -- Convert the first command line argument (a string) to an integer
 22    Integer_Text_IO.Get (From=>Arg, Item=>N, Last=>L);
 23 
 24    for I in 1 .. N loop
 25       Two_I := 2 * I;
 26       Sum := Two_I + 2*I**2;
 27       Integer_Text_IO.Put (Two_I+1, Width=>0);  -- A = 2*N+1
 28       Text_IO.Put (", ");
 29       Integer_Text_IO.Put (Sum, Width=>0);      -- B = 2*N+2*N**2
 30       Text_IO.Put (", ");
 31       Integer_Text_IO.Put (Sum+1, Width=>0);    -- C = 2*N+2*N**2 + 1
 32       Text_IO.New_Line;
 33    end loop;
 34 
 35 end Triples;