#!/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));
}
}
thor
The only easy day was yesterday
True, but it's fairly easy to filter out the non-primitives.
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.
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
perlmonks.org content © perlmonks.org and chas, jgamble, Pete_I, Scott7477, thor
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03