Law of cosines - triples: Difference between revisions
Content added Content deleted
(Added XPL0 example.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 39: | Line 39: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">-V n = 13 |
||
F method1(n) |
F method1(n) |
||
Line 69: | Line 69: | ||
V t60 = method1(10'000)[1] |
V t60 = method1(10'000)[1] |
||
V notsame = sum(t60.filter((a, b, c) -> a != b | b != c).map((a, b, c) -> 1)) |
V notsame = sum(t60.filter((a, b, c) -> a != b | b != c).map((a, b, c) -> 1)) |
||
print(‘Extra credit: ’notsame)</ |
print(‘Extra credit: ’notsame)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 84: | Line 84: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Test(INT max,angle,coeff) |
||
BYTE count,a,b,c |
BYTE count,a,b,c |
||
Line 109: | Line 109: | ||
Test(13,60,1) |
Test(13,60,1) |
||
Test(13,120,-1) |
Test(13,120,-1) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Law_of_cosines_-_triples.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Law_of_cosines_-_triples.png Screenshot from Atari 8-bit computer] |
||
Line 127: | Line 127: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
with Ada.Containers.Ordered_Maps; |
with Ada.Containers.Ordered_Maps; |
||
Line 196: | Line 196: | ||
Count_Triangles; |
Count_Triangles; |
||
Extra_Credit (Limit => 10_000); |
Extra_Credit (Limit => 10_000); |
||
end Law_Of_Cosines;</ |
end Law_Of_Cosines;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 228: | Line 228: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# find all integer sided 90, 60 and 120 degree triangles by finding integer solutions for # |
# find all integer sided 90, 60 and 120 degree triangles by finding integer solutions for # |
||
# a^2 + b^2 = c^2, a^2 + b^2 - ab = c^2, a^2 + b^2 + ab = c^2 where a, b, c in 1 .. 13 # |
# a^2 + b^2 = c^2, a^2 + b^2 - ab = c^2, a^2 + b^2 + ab = c^2 where a, b, c in 1 .. 13 # |
||
Line 275: | Line 275: | ||
print triangles( 90 ); |
print triangles( 90 ); |
||
print triangles( 120 ) |
print triangles( 120 ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 304: | Line 304: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f LAW_OF_COSINES_-_TRIPLES.AWK |
# syntax: GAWK -f LAW_OF_COSINES_-_TRIPLES.AWK |
||
# converted from C |
# converted from C |
||
Line 338: | Line 338: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 374: | Line 374: | ||
=={{header|C}}== |
=={{header|C}}== |
||
=== A brute force algorithm, O(N^3) === |
=== A brute force algorithm, O(N^3) === |
||
<syntaxhighlight lang="c">/* |
|||
<lang C>/* |
|||
* RossetaCode: Law of cosines - triples |
* RossetaCode: Law of cosines - triples |
||
* |
* |
||
Line 417: | Line 417: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 426: | Line 426: | ||
=== An algorithm with O(N^2) cost === |
=== An algorithm with O(N^2) cost === |
||
<syntaxhighlight lang="c">/* |
|||
<lang C>/* |
|||
* RossetaCode: Law of cosines - triples |
* RossetaCode: Law of cosines - triples |
||
* |
* |
||
Line 470: | Line 470: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 481: | Line 481: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <cmath> |
||
#include <iostream> |
#include <iostream> |
||
#include <tuple> |
#include <tuple> |
||
Line 557: | Line 557: | ||
<< min << " to " << max2 << " where the sides are not all of the same length.\n"; |
<< min << " to " << max2 << " where the sides are not all of the same length.\n"; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 574: | Line 574: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using static System.Linq.Enumerable; |
using static System.Linq.Enumerable; |
||
Line 616: | Line 616: | ||
private static bool NotAllTheSameLength((int a, int b, int c) triple) => triple.a != triple.b || triple.a != triple.c; |
private static bool NotAllTheSameLength((int a, int b, int c) triple) => triple.a != triple.b || triple.a != triple.c; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 649: | Line 649: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: backtrack formatting kernel locals math math.ranges |
||
sequences sets sorting ; |
sequences sets sorting ; |
||
IN: rosetta-code.law-of-cosines |
IN: rosetta-code.law-of-cosines |
||
Line 668: | Line 668: | ||
[ * + ] 120 |
[ * + ] 120 |
||
[ 2drop 0 - ] 90 |
[ 2drop 0 - ] 90 |
||
[ * - ] 60 [ show-solutions ] 2tri@</ |
[ * - ] 60 [ show-solutions ] 2tri@</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 698: | Line 698: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran">MODULE LAW_OF_COSINES |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
Line 850: | Line 850: | ||
END DO |
END DO |
||
END PROGRAM LOC</ |
END PROGRAM LOC</syntaxhighlight> |
||
<pre> |
<pre> |
||
90. degree triangles: |
90. degree triangles: |
||
Line 885: | Line 885: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 03-03-2019 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 958: | Line 958: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 15: 60 degree triangles |
<pre> 15: 60 degree triangles |
||
Line 993: | Line 993: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,066: | Line 1,066: | ||
fmt.Print(" For an angle of 60 degrees") |
fmt.Print(" For an angle of 60 degrees") |
||
fmt.Println(" there are", len(solutions), "solutions.") |
fmt.Println(" there are", len(solutions), "solutions.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,087: | Line 1,087: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import qualified Data.Map.Strict as Map |
||
import qualified Data.Set as Set |
import qualified Data.Set as Set |
||
import Data.Monoid ((<>)) |
import Data.Monoid ((<>)) |
||
Line 1,135: | Line 1,135: | ||
[120, 90, 60]) |
[120, 90, 60]) |
||
putStrLn "60 degrees - uneven triangles of maximum side 10000. Total:" |
putStrLn "60 degrees - uneven triangles of maximum side 10000. Total:" |
||
print $ length $ triangles f60ne 10000</ |
print $ length $ triangles f60ne 10000</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Triangles of maximum side 13 |
<pre>Triangles of maximum side 13 |
||
Line 1,171: | Line 1,171: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">load 'trig stats' |
||
RHS=: *: NB. right-hand-side of Cosine Law |
RHS=: *: NB. right-hand-side of Cosine Law |
||
LHS=: +/@:*:@] - cos@rfd@[ * 2 * */@] NB. Left-hand-side of Cosine Law |
LHS=: +/@:*:@] - cos@rfd@[ * 2 * */@] NB. Left-hand-side of Cosine Law |
||
Line 1,180: | Line 1,180: | ||
idx=. (RHS oppside) i. x LHS"1 adjsides |
idx=. (RHS oppside) i. x LHS"1 adjsides |
||
adjsides ((#~ idx ~: #) ,. ({~ idx -. #)@]) oppside |
adjsides ((#~ idx ~: #) ,. ({~ idx -. #)@]) oppside |
||
)</ |
)</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
< |
<syntaxhighlight lang="j"> 60 90 120 solve&.> 13 |
||
+--------+-------+------+ |
+--------+-------+------+ |
||
| 1 1 1|3 4 5|3 5 7| |
| 1 1 1|3 4 5|3 5 7| |
||
Line 1,201: | Line 1,201: | ||
+--------+-------+------+ |
+--------+-------+------+ |
||
60 #@(solve -. _3 ]\ 3 # >:@i.@]) 10000 NB. optional extra credit |
60 #@(solve -. _3 ]\ 3 # >:@i.@]) 10000 NB. optional extra credit |
||
18394</ |
18394</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java"> |
||
public class LawOfCosines { |
public class LawOfCosines { |
||
Line 1,249: | Line 1,249: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,285: | Line 1,285: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,414: | Line 1,414: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Triangles of maximum side 13: |
<pre>Triangles of maximum side 13: |
||
Line 1,454: | Line 1,454: | ||
To save space, we define `squares` as a hash rather than as a JSON array. |
To save space, we define `squares` as a hash rather than as a JSON array. |
||
< |
<syntaxhighlight lang="jq">def squares(n): |
||
reduce range(1; 1+n) as $i ({}; .[$i*$i|tostring] = $i); |
reduce range(1; 1+n) as $i ({}; .[$i*$i|tostring] = $i); |
||
Line 1,501: | Line 1,501: | ||
| " For an angle of \(degrees) degrees, there are \(.) solutions.") ; |
| " For an angle of \(degrees) degrees, there are \(.) solutions.") ; |
||
task1([90, 60, 120]), "", task2(60; 10000)</ |
task1([90, 60, 120]), "", task2(60; 10000)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,520: | Line 1,520: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|zkl}} |
{{trans|zkl}} |
||
< |
<syntaxhighlight lang="julia">sqdict(n) = Dict([(x*x, x) for x in 1:n]) |
||
numnotsame(arrarr) = sum(map(x -> !all(y -> y == x[1], x), arrarr)) |
numnotsame(arrarr) = sum(map(x -> !all(y -> y == x[1], x), arrarr)) |
||
Line 1,547: | Line 1,547: | ||
println("Angle 120:"); for t in tri120 println(t) end |
println("Angle 120:"); for t in tri120 println(t) end |
||
println("\nFor sizes N through 10000, there are $(numnotsame(filtertriangles(10000)[1])) 60 degree triples with nonequal sides.") |
println("\nFor sizes N through 10000, there are $(numnotsame(filtertriangles(10000)[1])) 60 degree triples with nonequal sides.") |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
Integer triples for 1 <= side length <= 13: |
Integer triples for 1 <= side length <= 13: |
||
Line 1,580: | Line 1,580: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.70 |
||
val squares13 = mutableMapOf<Int, Int>() |
val squares13 = mutableMapOf<Int, Int>() |
||
Line 1,647: | Line 1,647: | ||
print(" For an angle of 60 degrees") |
print(" For an angle of 60 degrees") |
||
println(" there are ${solutions.size} solutions.") |
println(" there are ${solutions.size} solutions.") |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 1,668: | Line 1,668: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function solve(angle, maxlen, filter) |
||
local squares, roots, solutions = {}, {}, {} |
local squares, roots, solutions = {}, {}, {} |
||
local cos2 = ({[60]=-1,[90]=0,[120]=1})[angle] |
local cos2 = ({[60]=-1,[90]=0,[120]=1})[angle] |
||
Line 1,692: | Line 1,692: | ||
solve(60, 10000, fexcr) -- extra credit |
solve(60, 10000, fexcr) -- extra credit |
||
solve(90, 10000, fexcr) -- more extra credit |
solve(90, 10000, fexcr) -- more extra credit |
||
solve(120, 10000, fexcr) -- even more extra credit</ |
solve(120, 10000, fexcr) -- even more extra credit</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>90° on 1..13 has 3 solutions |
<pre>90° on 1..13 has 3 solutions |
||
Line 1,722: | Line 1,722: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Solve[{a^2+b^2==c^2,1<=a<=13,1<=b<=13,1<=c<=13,a<=b},{a,b,c},Integers] |
||
Length[%] |
Length[%] |
||
Solve[{a^2+b^2-a b==c^2,1<=a<=13,1<=b<=13,1<=c<=13,a<=b},{a,b,c},Integers] |
Solve[{a^2+b^2-a b==c^2,1<=a<=13,1<=b<=13,1<=c<=13,a<=b},{a,b,c},Integers] |
||
Line 1,728: | Line 1,728: | ||
Solve[{a^2+b^2+a b==c^2,1<=a<=13,1<=b<=13,1<=c<=13,a<=b},{a,b,c},Integers] |
Solve[{a^2+b^2+a b==c^2,1<=a<=13,1<=b<=13,1<=c<=13,a<=b},{a,b,c},Integers] |
||
Length[%] |
Length[%] |
||
Solve[{a^2+b^2-a b==c^2,1<=a<=10000,1<=b<=10000,1<=c<=10000,a<=b,a!=b,b!=c,a!=c},{a,b,c},Integers]//Length</ |
Solve[{a^2+b^2-a b==c^2,1<=a<=10000,1<=b<=10000,1<=c<=10000,a<=b,a!=b,b!=c,a!=c},{a,b,c},Integers]//Length</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{{a->3,b->4,c->5},{a->5,b->12,c->13},{a->6,b->8,c->10}} |
<pre>{{a->3,b->4,c->5},{a->5,b->12,c->13},{a->6,b->8,c->10}} |
||
Line 1,739: | Line 1,739: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
import tables |
import tables |
||
Line 1,797: | Line 1,797: | ||
echo "\nFor sides in the range [1, 10000] where they cannot ALL be of the same length:\n" |
echo "\nFor sides in the range [1, 10000] where they cannot ALL be of the same length:\n" |
||
var solutions = solve(60, 10000, false) |
var solutions = solve(60, 10000, false) |
||
echo fmt" For an angle of 60 degrees there are {len(solutions)} solutions."</ |
echo fmt" For an angle of 60 degrees there are {len(solutions)} solutions."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,823: | Line 1,823: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use utf8; |
||
binmode STDOUT, "utf8:"; |
binmode STDOUT, "utf8:"; |
||
use Sort::Naturally; |
use Sort::Naturally; |
||
Line 1,856: | Line 1,856: | ||
} |
} |
||
printf "Non-equilateral n=10000/60°: %d\n", scalar triples(10000,60);</ |
printf "Non-equilateral n=10000/60°: %d\n", scalar triples(10000,60);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Integer triangular triples for sides 1..13: |
<pre>Integer triangular triples for sides 1..13: |
||
Line 1,866: | Line 1,866: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Using a simple flat sequence of 100 million elements (well within the desktop language limits, but beyond JavaScript) proved significantly faster than a dictionary (5x or so). |
Using a simple flat sequence of 100 million elements (well within the desktop language limits, but beyond JavaScript) proved significantly faster than a dictionary (5x or so). |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #000080;font-style:italic;">--constant lim = iff(platform()=JS?13:10000)</span> |
<span style="color: #000080;font-style:italic;">--constant lim = iff(platform()=JS?13:10000)</span> |
||
Line 1,910: | Line 1,910: | ||
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Non-equilateral angle 60 triangles for sides 1..10000: %d%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">),</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Non-equilateral angle 60 triangles for sides 1..10000: %d%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">),</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #000080;font-style:italic;">--end if</span> |
<span style="color: #000080;font-style:italic;">--end if</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 11px"> |
<pre style="font-size: 11px"> |
||
Line 1,923: | Line 1,923: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
< |
<syntaxhighlight lang="prolog">find_solutions(Limit, Solutions):- |
||
find_solutions(Limit, Solutions, Limit, []). |
find_solutions(Limit, Solutions, Limit, []). |
||
Line 1,986: | Line 1,986: | ||
write_triples(60, Solutions), |
write_triples(60, Solutions), |
||
write_triples(90, Solutions), |
write_triples(90, Solutions), |
||
write_triples(120, Solutions).</ |
write_triples(120, Solutions).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,002: | Line 2,002: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Sets=== |
===Sets=== |
||
< |
<syntaxhighlight lang="python">N = 13 |
||
def method1(N=N): |
def method1(N=N): |
||
Line 2,030: | Line 2,030: | ||
_, t60, _ = method1(10_000) |
_, t60, _ = method1(10_000) |
||
notsame = sum(1 for a, b, c in t60 if a != b or b != c) |
notsame = sum(1 for a, b, c in t60 if a != b or b != c) |
||
print('Extra credit:', notsame)</ |
print('Extra credit:', notsame)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,045: | Line 2,045: | ||
A variant Python draft based on dictionaries. |
A variant Python draft based on dictionaries. |
||
(Test functions are passed as parameters to the main function.) |
(Test functions are passed as parameters to the main function.) |
||
< |
<syntaxhighlight lang="python">from itertools import (starmap) |
||
Line 2,125: | Line 2,125: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Triangles of maximum side 13 |
<pre>Triangles of maximum side 13 |
||
Line 2,161: | Line 2,161: | ||
=={{header|R}}== |
=={{header|R}}== |
||
This looks a bit messy, but it really pays off when you see how nicely the output prints. |
This looks a bit messy, but it really pays off when you see how nicely the output prints. |
||
< |
<syntaxhighlight lang="rsplus">inputs <- cbind(combn(1:13, 2), rbind(seq_len(13), seq_len(13))) |
||
inputs <- cbind(A = inputs[1, ], B = inputs[2, ])[sort.list(inputs[1, ]),] |
inputs <- cbind(A = inputs[1, ], B = inputs[2, ])[sort.list(inputs[1, ]),] |
||
Pythagoras <- inputs[, "A"]^2 + inputs[, "B"]^2 |
Pythagoras <- inputs[, "A"]^2 + inputs[, "B"]^2 |
||
Line 2,175: | Line 2,175: | ||
sum(!is.na(output[, 3])), "solutions in the 90º case,", |
sum(!is.na(output[, 3])), "solutions in the 90º case,", |
||
sum(!is.na(output[, 4])), "solutions in the 60º case, and", |
sum(!is.na(output[, 4])), "solutions in the 60º case, and", |
||
sum(!is.na(output[, 5])), "solutions in the 120º case.")</ |
sum(!is.na(output[, 5])), "solutions in the 120º case.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> A B C (90º) C (60º) C (120º) |
<pre> A B C (90º) C (60º) C (120º) |
||
Line 2,203: | Line 2,203: | ||
(formerly Perl 6) |
(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>. |
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 |
<syntaxhighlight lang="raku" line>multi triples (60, $n) { |
||
my %sq = (1..$n).map: { .² => $_ }; |
my %sq = (1..$n).map: { .² => $_ }; |
||
my atomicint $i = 0; |
my atomicint $i = 0; |
||
Line 2,254: | Line 2,254: | ||
my ($angle, $count) = 60, 10_000; |
my ($angle, $count) = 60, 10_000; |
||
say "\nExtra credit:"; |
say "\nExtra credit:"; |
||
say "$angle° integer triples in the range 1..$count where the sides are not all the same length: ", +triples($angle, $count);</ |
say "$angle° integer triples in the range 1..$count where the sides are not all the same length: ", +triples($angle, $count);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Integer triangular triples for sides 1..13: |
<pre>Integer triangular triples for sides 1..13: |
||
Line 2,284: | Line 2,284: | ||
displayed (using the |
displayed (using the |
||
<br>absolute value of the negative number). |
<br>absolute value of the negative number). |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm finds integer sided triangles that satisfy Law of cosines for 60º, 90º, 120º.*/ |
||
parse arg os1 os2 os3 os4 . /*obtain optional arguments from the CL*/ |
parse arg os1 os2 os3 os4 . /*obtain optional arguments from the CL*/ |
||
if os1=='' | os1=="," then os1= 13; s1=abs(os1) /*Not specified? Then use the default.*/ |
if os1=='' | os1=="," then os1= 13; s1=abs(os1) /*Not specified? Then use the default.*/ |
||
Line 2,343: | Line 2,343: | ||
end /*b*/ |
end /*b*/ |
||
end /*a*/ |
end /*a*/ |
||
call foot s4; return</ |
call foot s4; return</syntaxhighlight> |
||
{{out|output|text= when using the default number of sides for the input: <tt> 13 </tt>}} |
{{out|output|text= when using the default number of sides for the input: <tt> 13 </tt>}} |
||
<pre> |
<pre> |
||
Line 2,384: | Line 2,384: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">grouped = (1..13).to_a.repeated_permutation(3).group_by do |a,b,c| |
||
sumaabb, ab = a*a + b*b, a*b |
sumaabb, ab = a*a + b*b, a*b |
||
case c*c |
case c*c |
||
Line 2,400: | Line 2,400: | ||
puts v.inspect, "\n" |
puts v.inspect, "\n" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>For an angle of 60 there are 15 solutions: |
<pre>For an angle of 60 there are 15 solutions: |
||
Line 2,412: | Line 2,412: | ||
</pre> |
</pre> |
||
Extra credit: |
Extra credit: |
||
< |
<syntaxhighlight lang="ruby">n = 10_000 |
||
ar = (1..n).to_a |
ar = (1..n).to_a |
||
squares = {} |
squares = {} |
||
Line 2,419: | Line 2,419: | ||
puts "There are #{count} 60° triangles with unequal sides of max size #{n}." |
puts "There are #{count} 60° triangles with unequal sides of max size #{n}." |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>There are 18394 60° triangles with unequal sides of max size 10000. |
<pre>There are 18394 60° triangles with unequal sides of max size 10000. |
||
Line 2,427: | Line 2,427: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ecmascript">var squares13 = {} |
||
var squares10000 = {} |
var squares10000 = {} |
||
Line 2,487: | Line 2,487: | ||
solutions = solve.call(60, 10000, false) |
solutions = solve.call(60, 10000, false) |
||
System.write(" For an angle of 60 degrees") |
System.write(" For an angle of 60 degrees") |
||
System.print(" there are %(solutions.count) solutions.")</ |
System.print(" there are %(solutions.count) solutions.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,508: | Line 2,508: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">proc LawCos(Eqn); |
||
int Eqn; |
int Eqn; |
||
int Cnt, A, B, C; |
int Cnt, A, B, C; |
||
Line 2,548: | Line 2,548: | ||
[for Case:= 1 to 3 do LawCos(Case); |
[for Case:= 1 to 3 do LawCos(Case); |
||
ExtraCredit; |
ExtraCredit; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,579: | Line 2,579: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn tritri(N=13){ |
||
sqrset:=[0..N].pump(Dictionary().add.fp1(True),fcn(n){ n*n }); |
sqrset:=[0..N].pump(Dictionary().add.fp1(True),fcn(n){ n*n }); |
||
tri90, tri60, tri120 := List(),List(),List(); |
tri90, tri60, tri120 := List(),List(),List(); |
||
Line 2,599: | Line 2,599: | ||
tri.sort(fcn(t1,t2){ t1[0]<t2[0] }) |
tri.sort(fcn(t1,t2){ t1[0]<t2[0] }) |
||
.apply("concat",",").apply("(%s)".fmt).concat(",") |
.apply("concat",",").apply("(%s)".fmt).concat(",") |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">N:=13; |
||
println("Integer triangular triples for sides 1..%d:".fmt(N)); |
println("Integer triangular triples for sides 1..%d:".fmt(N)); |
||
foreach angle, triples in (T(60,90,120).zip(tritri(N))){ |
foreach angle, triples in (T(60,90,120).zip(tritri(N))){ |
||
println(" %3d\U00B0; has %d solutions:\n %s" |
println(" %3d\U00B0; has %d solutions:\n %s" |
||
.fmt(angle,triples.len(),triToStr(triples))); |
.fmt(angle,triples.len(),triToStr(triples))); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,617: | Line 2,617: | ||
</pre> |
</pre> |
||
Extra credit: |
Extra credit: |
||
< |
<syntaxhighlight lang="zkl">fcn tri60(N){ // special case 60* |
||
sqrset:=[1..N].pump(Dictionary().add.fp1(True),fcn(n){ n*n }); |
sqrset:=[1..N].pump(Dictionary().add.fp1(True),fcn(n){ n*n }); |
||
n60:=0; |
n60:=0; |
||
Line 2,625: | Line 2,625: | ||
} |
} |
||
n60 |
n60 |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">N:=10_000; |
||
println(("60\U00b0; triangle where side lengths are unique,\n" |
println(("60\U00b0; triangle where side lengths are unique,\n" |
||
" side lengths 1..%,d, there are %,d solutions.").fmt(N,tri60(N)));</ |
" side lengths 1..%,d, there are %,d solutions.").fmt(N,tri60(N)));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |