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) Source: “Pythagorean Triple” Wikipedia. Last Modified February 7, 2017. https://en.wikipedia.org/wiki/Pythagorean_triple Accessed February 7, 2017 |
|||
02-09-2017, 08:49 AM
(This post was last modified: 02-09-2017 08:56 AM by Dieter.)
Post: #2
|
|||
|
|||
RE: Pythagorean Triples
(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 |
|||
02-09-2017, 02:49 PM
Post: #3
|
|||
|
|||
RE: Pythagorean Triples
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. <0|ɸ|0> -Joe- |
|||
02-09-2017, 08:57 PM
(This post was last modified: 02-09-2017 08:59 PM by Dieter.)
Post: #4
|
|||
|
|||
RE: Pythagorean Triples
(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 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)