How to find the first Pythagorean triple in Pascal

1 Answer

0 votes
program FirstPythagoreanTriple;

{$mode objfpc}{$H+}

{
A Pythagorean triple is a set of three positive integers (a, b, c)
that satisfy the Pythagorean theorem:

        a^2 + b^2 = c^2

These represent the side lengths of a right triangle.
The smallest and most famous Pythagorean triple is (3, 4, 5).
}

uses
  Math;

{ Function to find the first Pythagorean triple }
procedure FindFirstPythagoreanTriple;
var
  a, b, c2, c: Integer;
begin
  for a := 1 to 100 do
    for b := a to 100 do
    begin
      c2 := a * a + b * b;   { c^2 }
      c := Trunc(Sqrt(c2));

      if c * c = c2 then     { Check if c is an integer }
      begin
        WriteLn('First Pythagorean triple: ', a, ', ', b, ', ', c);
        Exit;
      end;
    end;
end;

begin
  FindFirstPythagoreanTriple;
end.



(*
run:

First Pythagorean triple: 3, 4, 5

*)

 



answered Jun 19 by avibootz
...