## Wednesday, February 8, 2017

### HP Prime, TI-84 Plus CE, Sharp EL-5500 III: Pythagorean Triples

HP Prime, TI-84 Plus CE, Sharp EL-5500 III:  Pythagorean Triples

Criteria

The program PYTHA calculates a Pythagorean triple.  A Pythagorean triple is a set of three positive integers A, B, and C that represent the lengths of a right triangle, with C being the hypotenuse.  Hence, A^2 + B^2 = C^2.

Pythagorean triples can be generated with three arbitrary positive integers M, N, and K with the following criteria:

1.  M > N
2.  M and N are coprime.  That is, gcd(M, N) = 1 (gcd, greatest common denominator)

A, B, and C are generated by:

A = K * (M^2 – N^2)
B = K * (2 * M * N)
C = K * (M^2 + N^2)

Verification

We can verify that the above formulas for A, B, and C work by showing A^2 + B^2 = C^2.

A^2 + B^2
= (K * (M^2 – N^2))^2 + (K * 2 * M * N)^2
= (K * M^2 – K * N^2)^2  + 4 * K^2 * M^2 * N^2
= K^2 * M^4 – 2 * K^2 * M^2 * N^2 + K^2 * N^2 + 4 * K^2 * M^2 * N^2
= K^2 * M^4 + 2 * K^2 * M^2 * N^2 + K^2 * N^2
= (K * M^2 + K * N^2)^2
= K^2 * (M^2 + N^2)^2
= C^2

Programs

HP Prime Program PYTHA

EXPORT PYTHA(M,N,K)
BEGIN
// 2017-02-08 EWS
// Pythagorean Triangle
LOCAL A,B,C;

// checks (not for minimum)
M:=IP(M); N:=IP(N); K:=IP(K);

IF M≤0 OR N≤0 OR
gcd(M,N)≠1 OR M≤N THEN
RETURN "INVALID"; KILL;
END;

// calculations
A:=K*(M^2-N^2);
B:=K*(2*M*N);
C:=K*(M^2+N^2);

RETURN {A,B,C};
END;

TI-84 Plus Program PYTHA

"EWS 2017-02-08"
Prompt M,N,K
iPart(M)→M
iPart(N)→N
iPart(K)→K
If M≤0 or N≤0 or K≤0 or M≤N or gcd(M,N)≠1
Then
Disp "INVALID"
Stop
End
K*(M²-N²)→A
K*(2*M*N)→B
K*(M²+N²)→C
Pause {A,B,C}

Sharp EL-5500 III Pythagorean Triple
(RUN 450 (line numbers are arbitrary))

450 PAUSE “Pythagorean Triple”
455 INPUT “M:”; M, “N:”; N, “K:”; K
460 IF M<=0 OR N<=0 OR K<=0 OR M<=N THEN 480
462 A = K*(M^2 - N^2)
464 B = K*2*M*N
466 C = K*(M^2 + N^2)
468 IF A^2 + B^2 <> C^2 THEN 480
470 PRINT A; “^2+”; B; “^2=”; C; “^2”
472 END
480 PRINT “INVALID”
482 END

Examples:

M: 2, N: 1, K: 1. Result:  A: 3, B: 4, C: 5

M: 7, N: 2, K: 1. Result:  A: 45, B: 28, C: 53

M: 5, N: 3, K: 2. Result:  A: 32, B: 60, C: 68

Source: