A Stream script to generate pythagorian triples.
#!/usr/bin/perl use Data::Dumper; use Stream; sub nplusone { my ($m,$n) = @{$_[0]}; $n == $m+1 ? [1,$n+1] : [$m+1,$n]; } sub ptrip { my ($m,$n) = @{$_[0]}; [ ($n**2-$m**2), (2*$n*$m) , ($n**2+$m**2) ]; } @triples = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } map { [ sort {$a <=> $b} @$_ ] } iterate(\&nplusone,[1,2])->transform(\&ptrip)->take(1000); print Dumper(\@triples);

Replies are listed 'Best First'.
Re: Pythagorean Triples
by Zaxo (Archbishop) on Dec 05, 2002 at 13:54 UTC

    Well, somebody's got to do the mathmonk thing.

    This has a neat relation to number theory. Prime numbers which satisfy (1 == $prime % 4) can be expressed as a sum of squares, ($m**2 + $n**2). Such primes can be factored as complex numbers, $n+i*$m and $n-i*$m with $n > $m. These are known as Gaussian primes.

    Plugging $m and $n into your formula shows the correspondence of primes equal to 4*$foo + 1 and Pythagorean right triangles. The ever-popular 3-4-5 right triangle corresponds to the Gaussian prime (2 + i).

    Not every Pythagorean right triangle corresponds to a Gaussian prime. If $n and $m have a common factor, or are both odd, then they do not represent a complex prime in this way.

    After Compline,
    Zaxo

(OT) Re: Pythagorean Triples
by japhy (Canon) on Dec 05, 2002 at 14:42 UTC
    My fraternity's mentor is Pythagoras, and our chief symbol is the right-angled triangle whose sides are proportional to 3, 4, and 5.

    Just thought I'd chime in with that. :)

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;