Pythagorean triples: Difference between revisions
m
→{{header|Perl 6}}: Tweak brute-force example a bit
m (→{{header|Java}}: Added link to new brute force version) |
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Tweak brute-force example a bit) |
||
Line 461:
First up is a fairly naive brute force implementation that is not really practical for large perimeter limits. 100 is no problem. 1000 is slow. 10000 is glacial.
Works with Rakudo
<lang perl6>my %
my $limit = 100;
for 3 .. $limit/2 -> $c {
for 1 .. $c
my $b = ($c
last if $c + $a + $b > $limit;
last if $a > $b;
if $b == $b.Int {
my $key =
say $key, %
}
}
}
say "There are {+%
~"
<pre>There are 17 Pythagorean Triples with a perimeter less than 100, of which 7 are primitive.▼
▲ 3 4 5 - primitive
7 24
▲ 6 8 10
▲ 7 24 25 - primitive
20 21
▲ 9 12 15
9 40 41 - primitive▼
▲ 10 24 26
12 35 37 - primitive
▲ 12 16 20
▲ 12 35 37 - primitive
▲ 15 20 25
▲ 15 36 39
▲ 16 30 34
▲ 18 24 30
</pre>
▲ 20 21 29 - primitive
▲ 21 28 35
▲ 24 32 40</pre>
Here's a much faster version. Hint, "oyako" is Japanese for "parent/child". <tt>:-)</tt>
{{works with|niecza}}
|