Law of cosines - triples: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 212:
7 8 13
</pre>
 
=={{header|AWK}}==
<lang AWK>
Line 437 ⟶ 438:
}
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 03-03-2019
Line 1,275 ⟶ 1,277:
Angle 60° has 15 solutions: 1 1 1, 2 2 2, 3 3 3, 3 7 8, 4 4 4, 5 5 5, 5 7 8, 6 6 6, 7 7 7, 8 8 8, 9 9 9, 10 10 10, 11 11 11, 12 12 12, 13 13 13
Non-equilateral n=10000/60°: 18394</pre>
 
=={{header|Perl 6}}==
In each routine, <tt>race</tt> is used to allow concurrent operations, requiring the use of the atomic increment operator, <tt>⚛++</tt>, to safely update <tt>@triples</tt>, which must be declared fixed-sized, as an auto-resizing array is not thread-safe. At exit, default values in <tt>@triples</tt> are filtered out with the test <code>!eqv Any</code>.
<lang perl6>multi triples (60, $n) {
my %sq = (1..$n).map: { .² => $_ };
my atomicint $i = 0;
my @triples[2*$n];
(1..^$n).race(:8degree).map: -> $a {
for $a^..$n -> $b {
my $cos = $a * $a + $b * $b - $a * $b;
@triples[$i⚛++] = $a, %sq{$cos}, $b if %sq{$cos}:exists;
}
}
@triples.grep: so *;
}
 
multi triples (90, $n) {
my %sq = (1..$n).map: { .² => $_ };
my atomicint $i = 0;
my @triples[2*$n];
(1..^$n).race(:8degree).map: -> $a {
for $a^..$n -> $b {
my $cos = $a * $a + $b * $b;
@triples[$i⚛++] = $a, $b, %sq{$cos} and last if %sq{$cos}:exists;
}
}
@triples.grep: so *;
}
 
multi triples (120, $n) {
my %sq = (1..$n).map: { .² => $_ };
my atomicint $i = 0;
my @triples[2*$n];
(1..^$n).race(:8degree).map: -> $a {
for $a^..$n -> $b {
my $cos = $a * $a + $b * $b + $a * $b;
@triples[$i⚛++] = $a, $b, %sq{$cos} and last if %sq{$cos}:exists;
}
}
@triples.grep: so *;
}
 
use Sort::Naturally;
 
my $n = 13;
say "Integer triangular triples for sides 1..$n:";
for 120, 90, 60 -> $angle {
my @itt = triples($angle, $n);
if $angle == 60 { push @itt, "$_ $_ $_" for 1..$n }
printf "Angle %3d° has %2d solutions: %s\n", $angle, +@itt, @itt.sort(*.&naturally).join(', ');
}
 
my ($angle, $count) = 60, 10_000;
say "\nExtra credit:";
say "$angle° integer triples in the range 1..$count where the sides are not all the same length: ", +triples($angle, $count);</lang>
{{out}}
<pre>Integer triangular triples for sides 1..13:
Angle 120° has 2 solutions: 3 5 7, 7 8 13
Angle 90° has 3 solutions: 3 4 5, 5 12 13, 6 8 10
Angle 60° has 15 solutions: 1 1 1, 2 2 2, 3 3 3, 3 7 8, 4 4 4, 5 5 5, 5 7 8, 6 6 6, 7 7 7, 8 8 8, 9 9 9, 10 10 10, 11 11 11, 12 12 12, 13 13 13
 
Extra credit:
60° integer triples in the range 1..10000 where the sides are not all the same length: 18394</pre>
 
=={{header|Phix}}==
Line 1,545 ⟶ 1,484:
60 degrees - uneven triangles of maximum side 10000. Total:
18394</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
In each routine, <tt>race</tt> is used to allow concurrent operations, requiring the use of the atomic increment operator, <tt>⚛++</tt>, to safely update <tt>@triples</tt>, which must be declared fixed-sized, as an auto-resizing array is not thread-safe. At exit, default values in <tt>@triples</tt> are filtered out with the test <code>!eqv Any</code>.
<lang perl6>multi triples (60, $n) {
my %sq = (1..$n).map: { .² => $_ };
my atomicint $i = 0;
my @triples[2*$n];
(1..^$n).race(:8degree).map: -> $a {
for $a^..$n -> $b {
my $cos = $a * $a + $b * $b - $a * $b;
@triples[$i⚛++] = $a, %sq{$cos}, $b if %sq{$cos}:exists;
}
}
@triples.grep: so *;
}
 
multi triples (90, $n) {
my %sq = (1..$n).map: { .² => $_ };
my atomicint $i = 0;
my @triples[2*$n];
(1..^$n).race(:8degree).map: -> $a {
for $a^..$n -> $b {
my $cos = $a * $a + $b * $b;
@triples[$i⚛++] = $a, $b, %sq{$cos} and last if %sq{$cos}:exists;
}
}
@triples.grep: so *;
}
 
multi triples (120, $n) {
my %sq = (1..$n).map: { .² => $_ };
my atomicint $i = 0;
my @triples[2*$n];
(1..^$n).race(:8degree).map: -> $a {
for $a^..$n -> $b {
my $cos = $a * $a + $b * $b + $a * $b;
@triples[$i⚛++] = $a, $b, %sq{$cos} and last if %sq{$cos}:exists;
}
}
@triples.grep: so *;
}
 
use Sort::Naturally;
 
my $n = 13;
say "Integer triangular triples for sides 1..$n:";
for 120, 90, 60 -> $angle {
my @itt = triples($angle, $n);
if $angle == 60 { push @itt, "$_ $_ $_" for 1..$n }
printf "Angle %3d° has %2d solutions: %s\n", $angle, +@itt, @itt.sort(*.&naturally).join(', ');
}
 
my ($angle, $count) = 60, 10_000;
say "\nExtra credit:";
say "$angle° integer triples in the range 1..$count where the sides are not all the same length: ", +triples($angle, $count);</lang>
{{out}}
<pre>Integer triangular triples for sides 1..13:
Angle 120° has 2 solutions: 3 5 7, 7 8 13
Angle 90° has 3 solutions: 3 4 5, 5 12 13, 6 8 10
Angle 60° has 15 solutions: 1 1 1, 2 2 2, 3 3 3, 3 7 8, 4 4 4, 5 5 5, 5 7 8, 6 6 6, 7 7 7, 8 8 8, 9 9 9, 10 10 10, 11 11 11, 12 12 12, 13 13 13
 
Extra credit:
60° integer triples in the range 1..10000 where the sides are not all the same length: 18394</pre>
 
=={{header|REXX}}==
10,327

edits