Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pythagorean Triples
09-19-2009, 05:37 PM
Post: #1
Pythagorean Triples
Every triangle with one angle 90 degrees has 3 sides that must satisfy relationship that sum of the squares of 2 sides equals to the square of the hypotenuse. For example (3,4,5) is a Pythagorean triple because
sqr(3)+sqr(4)=sqr(5) or
9+16=25
Your task is to find triples that are not bigger than 20, and you need to calculate repetition inside of this algorithm ! And you need to sort triples from smallest to biggest !
So here is the code in TPW :
Code:
program pythagorean_triples;
uses wincrt;
type
   tarray=record
   x,y,z:longint;
end;
var
  i1,i2,i3,br,i:longint;
  a:array[1..200] of tarray;
function check(i1,i2,i3:longint):boolean;
var
  i:longint;
  b:boolean;
begin
  if br = 0 then
  check:=true
  else
  begin
  b:=false;
  for i:=1 to br do
   if b=false then
    begin
     if (i1=a[i].x) and (i2=a[i].y) and (i3=a[i].z) then
     b:=true;
     if (i1=a[i].y) and (i2=a[i].x) and (i3=a[i].z) then
     b:=true;
     if (i1=a[i].x) and (i2=a[i].z) and (i3=a[i].y) then
     b:=true;
     if (i1=a[i].y) and (i2=a[i].z) and (i3=a[i].x) then
     b:=true;
     if (i1=a[i].z) and (i2=a[i].y) and (i3=a[i].x) then
     b:=true;
     if (i1=a[i].z) and (i2=a[i].x) and (i3=a[i].y) then
     b:=true;
    end;
    if b=false then
    check:=true
    else
    check:=false;
  end;
end;
begin
br:=0;
for i1:=1 to 20 do
for i2:=1 to 20 do
for i3:=1 to 20 do
begin
if sqr(i1)+sqr(i2)=sqr(i3) then
if check(i1,i2,i3) then
begin
  br:=br+1;
  a[br].x:=i1;
  a[br].y:=i2;
  a[br].z:=i3;
end;
end;
for i:=1 to br do
begin
writeln(a[i].x,' ',a[i].y,' ',a[i].z);
end;
end.
As you can see there are 6 solutions of triples inside first 20 numbers :
3 4 5
5 12 13
6 8 10
8 15 17
9 12 15
12 16 20

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
12-19-2009, 07:09 AM
Post: #2
RE: Pythagorean Triples
Since i didn't write mathematical formulation of Pythagorean Triples, i'll do it now.
Pythagorean Triple is a triple from natural number triples which has a condition of satisfying Pythagorean theorem.
More about Natural numbers (set of Natural numbers N) you can find here :
http://en.wikipedia.org/wiki/Natural_number
And about Pythagorean theorem, you can find here :
http://en.wikipedia.org/wiki/Pythagorean_theorem
So mathematical formulation would look like this :

[Image: 26651_pythagoreantriples.jpg]

Where N^3 is a sign for triple in set of Natural numbers.
V is mathematical "or".
So set of triples "S" are all Pythagorean Triples that can be generated in set of Natural numbers.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: