Post Reply 
Pythagorean Triples
02-09-2017, 05:42 AM
Post: #1
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)


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
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Pythagorean Triples - Eddie W. Shore - 02-09-2017 05:42 AM
RE: Pythagorean Triples - Dieter - 02-09-2017, 08:49 AM
RE: Pythagorean Triples - Joe Horn - 02-09-2017, 02:49 PM
RE: Pythagorean Triples - Dieter - 02-09-2017, 08:57 PM



User(s) browsing this thread: 1 Guest(s)