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) 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: 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" 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 |