Pythagorean triples: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: add oyako algorithm)
Line 451: Line 451:
21 28 35
21 28 35
24 32 40</pre>
24 32 40</pre>
Here's a much faster version. Hint, "oyako" is Japanese for "parent/child". <tt>:-)</tt>
{{works with|niecza}}
<lang perl6>sub triples($limit) {

sub oyako($a, $b, $c) {
my $perim = $a + $b + $c;
return if $perim > $limit;
take 1 + ($limit div $perim)i;
oyako( $a - 2*$b + 2*$c, 2*$a - $b + 2*$c, 2*$a - 2*$b + 3*$c);
oyako( $a + 2*$b + 2*$c, 2*$a + $b + 2*$c, 2*$a + 2*$b + 3*$c);
oyako(-$a + 2*$b + 2*$c, -2*$a + $b + 2*$c, -2*$a + 2*$b + 3*$c);
}

my $complex = 0i + [+] gather oyako(3,4,5);
"$limit => ({$complex.re, $complex.im})";
}

for 10,100,1000 ... * -> $limit {
say triples $limit;</lang>
Output:
<pre>10 => (0 0)
100 => (7 17)
1000 => (70 325)
10000 => (703 4858)
100000 => (7026 64741)
1000000 => (70229 808950)
10000000 => (702309 9706567)
^C</pre>
Note the cute trick of adding complex numbers to add two numbers in parallel. Also, it will continue on forever, so eventually when you get tired of thrashing (at about 100 million on my computer), you can just stop it.


=={{header|Python}}==
=={{header|Python}}==