HP Forums
Pythagorean Triples - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: HP Prime Software Library (/forum-15.html)
+--- Thread: Pythagorean Triples (/thread-7746.html)



Pythagorean Triples - Eddie W. Shore - 02-09-2017 05:42 AM

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)


Code:
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;

Source:

“Pythagorean Triple” Wikipedia. Last Modified February 7, 2017.
https://en.wikipedia.org/wiki/Pythagorean_triple
Accessed February 7, 2017


RE: Pythagorean Triples - Dieter - 02-09-2017 08:49 AM

(02-09-2017 05:42 AM)Eddie W. Shore Wrote:  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)

Eddie, M and N do not have to be coprime (which can be easily shown). If they are, K=1 produces the smallest possible Pythagorean triple. But this is not required for generating such triples in general. Any M > N will do. If the GCD of M and N is G instead of 1 the result is the same as if you would use G²*K instead of K in your formula.

Example: M=10 and N=20 yields 300² + 400² = 500².
Which is 10² times the result of M=1 and N=2, leading to 3² + 4² = 5².

So the GCD condition can be dropped. If your goal is generating primitive Pythagorean triples, a third condition has to be added: M and N must not be both odd, it has to be one odd and one even value, cf. the Wikipedia article you linked to.

Dieter


RE: Pythagorean Triples - Joe Horn - 02-09-2017 02:49 PM

Pythagorean Triple Generator in RPL:

« → X Y « X SQ Y SQ + LASTARG - ABS X Y * 2 * » »

Inputs: two unequal integers > 0. If a "primitive" Pythagorean triple is desired, the inputs must also be coprime and one of them must be even.


RE: Pythagorean Triples - Dieter - 02-09-2017 08:57 PM

(02-09-2017 02:49 PM)Joe Horn Wrote:  Pythagorean Triple Generator in RPL:

Here's a stack-only version for the '41:

Code:
01 LBL"TRIPLE"
02 x^2
03 STO Z
04 x<>y
05 x^2
06 STO T
07 -
08 ABS
09 RDN
10 ST* Z
11 +
12 x<>y
13 SQRT
14 ST+ X
15 R^
16 END

Input is N ENTER M, output is a, b and c in X, Y and Z.

This may also be used on the 42s.
Just make sure all registers refer to the stack, i.e. STO Z means STO ST Z.

And I am sure you can still squeeze out the one or other byte here. ;-)

Dieter