Category: 
Miscellaneous 
Author/Contact Info 
 
Description: 
calculates pythagorean triples
explanation:
a pythagorean triple is a set of nonzero integers that make the pythagorean theorem return true when they're plugged in.
3,4,5 is the first triple.
sqrt((3)**2 + (4)**2) = 25
sqrt(9 + 16) = 25
sqrt(25) == 5
I've got no idea what uses this could have, but I thought it was interesting.
it looked cleaner at the beginning, but map{} and for(a..b) had big memory problems. If you find a use for it, i'd like to know. Enjoy :) 
#!/usr/bin/perl
#generate pythagorean triples until n = ARGV[1]
#based on this: http://www.math.uic.edu/~fields/puzzle/triples.html
#a, b, c = n^2  m^2, 2*m*n, n^2 + m^2
my($m, $n, $end);
$end = $ARGV[0]  5;
for($n = 1; $n <= $end; $n++) {
for($m = 1; $m < $n; $m++) {
printf("%d,%d,%d\n", ($n * $n  $m * $m), (2 * $m * $n), ($n * $n +
+$m * $m));
}
}

Re: pythagorean triples
by Scott7477 (Chaplain) on Apr 24, 2006 at 03:37 UTC

Any perlmonks who happen to be members of what Wikipedia refers to as "the mysterious religious and scientific society called Pythagoreans" might like this;) Maybe it's useful for forecasting the future:)..The Wikipedia page Pythagoras states that "Pythagoras and his students believed that everything was related to mathematics and thought that everything could be predicted and measured in rhythmic patterns or cycles."  [reply] 
Re: pythagorean triples
by chas (Priest) on Apr 24, 2006 at 03:16 UTC

But that doesn't really give "all" of them  e.g. 9,12,15 isn't of that form, is it? (Of course it is a multiple of 3,4,5 which is of that form...)  [reply] 

That's correct, but any integer multiple of a primitive Pythagorean triple is also a Pythagorean triple. (The proof is left as an exercise for the reader) This sequence generates the set of primitive Pythagorean triples (assuming that $m & $n are coprime)
thor
The only easy day was yesterday
 [reply] 

Well, it's true that the primitive triples are of that form,
but not conversely; 6,8,10 occurs for n=3,m=1 (which are coprime), and that triple isn't primitive. So the code generates primitive (i.e. having no common factor) triples, but some others as well. The real point is that it isn't so easy to print a list of all Pythagorean triples without duplication, and I guess that thought was what motivated my reply.
 [reply] 

True, but it's fairly easy to filter out the nonprimitives.
If m and n are both even, skip (a, b, and c will all be divisible by two).
If m and n are both odd, skip (a, b, and c will all be divisible by two).
If m and n have a divisor in common (e.g., GCD{12,3} != 1) skip (a, b, and c will have that factor in common).
The first case can be wrapped up in third case (actually, it can be wrapped up in the second case too), but I find that it makes things clearer to separate them out.
 [reply] 

The third case should of course have been written as:
If m and n have a divisor in common (e.g., GCD{m,n} != 1) skip (a, b, and c will have that divisor in common).
Sorry about that
 [reply] 

