Pythagorean triples: Difference between revisions

→‎{{header|Perl 6}}: add vectorized version
(→‎{{header|Perl 6}}: simplify obtaining the number of pairs in a hash)
(→‎{{header|Perl 6}}: add vectorized version)
Line 480:
^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.
 
Here is an alternate version that avoids naming any scalars that can be handled by vector processing instead:
<lang perl6>constant @coeff = [[+1, -2, +2], [+2, -1, +2], [+2, -2, +3]],
[[+1, +2, +2], [+2, +1, +2], [+2, +2, +3]],
[[-1, +2, +2], [-2, +1, +2], [-2, +2, +3]];
 
sub triples($limit) {
sub oyako(@trippy) {
my $perim = [+] @trippy;
return if $perim > $limit;
take (1 + ($limit div $perim)i);
for @coeff -> @nine {
oyako (map -> @three { [+] @three »*« @trippy }, @nine);
}
return;
}
 
my $complex = 0i + [+] gather oyako([3,4,5]);
"$limit => ({$complex.re, $complex.im})";
}
 
for 10,100,1000 ... * -> $limit {
say triples $limit;
}</lang>
 
=={{header|Python}}==
Anonymous user