Sunday, July 12, 2020

HP Prime: Pythagorean Triangle Search

HP Prime: Pythagorean Triangle Search

Three Integers Make a Right Triangle

A Pythagorean triple is a trio of positive integers a, b, and c, that describe the lengths of a right triangle, where a and b are the lengths of the sides while c is the length of the hypotenuse. 

The variables a, b, and c follow the Pythagorean Theorem:

a^2 + b^2 = c^2

Where the following measurements perimeter and area are calculated as:

p = perimeter = a + b + c
r = area = a * b / 2

With two positive integers m and n where m > n, Euclid gives a formula where a, b, and c are generated:

a = 2 * m * n
b = m^2 - n^2
c = m^2 + n^2

This can easily verified to satisfy the Pythagorean Theorem:

a^2 + b^2 = c^2
(2*m*n)^2 + (m^2 - n^2)^2 = (m^2 + n^2)^2
4*m^2*n^2 + m^4 - 2*m^2*m^2 + n^4 = m^4 + 2*m^2*m^2 + n^4
4*m^2*n^2 + m^4 - 4*m^2*m^2 + n^4 = m^4 + n^4
m^4 + n^4 = m^4 + n^4

The following program PYTHRI asks you for m and n and generates a Pythagorean triple.  Make sure that m > n. 

HP Prime Program PYTHRI

EXPORT PYTHTRI()
BEGIN
// 2020-06-13 EWS
// r: area
LOCAL a,b,c,p,r,m,n;
INPUT({m,n},"Pythagorean Triple
 Generator",{"m = ","n = "},
{"m > n, m,n ∈ Z+","m > n, m,n ∈ Z+"});
a:=2*m*n; b:=m^2-n^2; c:=m^2+n^2;
p:=a+b+c; r:=a*b/2;
PRINT();
PRINT(a+"^2+"+b+"^2="+c+"^2");
PRINT("a = "+a);
PRINT("b = "+b);
PRINT("c = "+c);
PRINT("perimeter = "+p);
PRINT("area = "+r);
END;

Can We Go the Other Way?

Let's say we have the area and the perimeter of a right triangle.  Can we find a Pythagorean triple?  In order to do so, we need to solve for m and n, and make sure that m and n are positive integers. 

Recall that:

a = 2 * m * n
b = m^2 - n^2
c = m^2 + n^2

Perimeter:

p = a + b + c
p = 2 * m * n + m^2 - n^2 + m^2 + n^2
p = 2 * m^2 + 2 * m * n

Area:

r = a * b / 2
r = m * n * (m^2 - n^2)
r = m^3 * n - m * n^3

Let's solve for n in the perimeter equation:

p = 2 * m^2 + 2 * m * n
p - 2 * m^2 = 2 * m * n
Since m is a positive integer, m ≠ 0 and by dividing by 2 * m:
p / (2* m) - m = n

Substitute in the area equation:

r = m^3 * n - m * n^3
r = m^3 * (p / (2* m) - m) - m * (p / (2* m) - m)^3

The program IPYTHTRI attempts to find a Pythagorean triple by solving for m in the above equation.   A first initial guess of 0 is used, but the initial guess  uses powers of 10 for any further iterations that are needed. 

Should a triple not be found, the program will indicate the finding.  Perfect search is not guaranteed.

If a suitable solution is found, then the program calculates and displays a, b, and c.

HP Prime Program IPYTHTRI
(inverse PYTHTRI)

EXPORT IPYTHTRI()
BEGIN
// 2020-06-13 EWS
// r: area
LOCAL a,b,c,p,r,m,n,k;
INPUT({p,a},"Pythagorean Triple
 Search",{"p = ","r = "},
{"perimeter","area"});
// search for integers
FOR k FROM 0 TO 7 DO
m:=fsolve(X^3*(p/(2*X)-X)
-X*(p/(2*X)-X)^3-a,X,10*k);
IF (FP(m)==0) AND (m>0) THEN
BREAK;
END;
END;
n:=p/(2*m)-m;
PRINT();
PRINT("m = "+m);
PRINT("n = "+n);
IF (FP(m)==0) AND (FP(n)==0) THEN
PRINT("Integer Solutions Found");
a:=2*m*n; b:=m^2-n^2; c:=m^2+n^2;
PRINT(a+"^2+"+b+"^2="+c+"^2");
PRINT("a = "+a);
PRINT("b = "+b);
PRINT("c = "+c);
ELSE
PRINT("No integer solutions found");
END;
END;

Examples
(p = perimeter, r = area)

m = 5, n = 3
a = 30, b = 16, c = 34
p = 80, r = 240

m = 11, n = 6
a = 132, b = 85, c = 157
p = 374, r = 5610

m = 18, n = 14
a = 504, b = 128, c = 520
p = 1152, r = 32256

m = 164, n = 133
a = 43624, b = 9207, c = 44585
p = 97416, r = 200,823,084

Source:
Pythagorean triple.  Wikipedia.  Last Edited June 13, 2020.  https://en.wikipedia.org/wiki/Pythagorean_triple  Accessed June 13, 2020

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...