Law of cosines - triples: Difference between revisions

(Added R.)
Line 1,404:
18394
[Finished in 3.444s]</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
To save space, we define `squares` as a hash rather than as a JSON array.
<lang jq>def squares(n):
reduce range(1; 1+n) as $i ({}; .[$i*$i|tostring] = $i);
 
# if count, then just count
def solve(angle; maxLen; allowSame; count):
squares(maxLen) as $squares
 
| def qsqrt($n):
$squares[$n|tostring] as $sqrt
| if $sqrt then $sqrt else null end;
 
reduce range(1; maxLen+1) as $a ({};
reduce range($a; maxLen+1) as $b (.;
.lhs = $a*$a + $b*$b
| if angle != 90
then if angle == 60
then .lhs += ( - $a*$b)
elif angle == 120
then .lhs += $a*$b
else "Angle must be 60, 90 or 120 degrees" | error
end
else .
end
| qsqrt(.lhs) as $c
| if $c != null
then if allowSame or $a != $b or $b != $c
then .solutions += if count then 1 else [[$a, $b, $c]] end
else .
end
else .
end
)
)
| .solutions ;
 
def task1($angles):
"For sides in the range [1, 13] where they can all be of the same length:\n",
($angles[]
| . as $angle
| solve($angle; 13; true; false)
| " For an angle of \($angle) degrees, there are \(length) solutions, namely:", .);
 
def task2(degrees; n):
"For sides in the range [1, \(n)] where they cannot ALL be of the same length:",
(solve(degrees; n; false; true)
| " For an angle of \(degrees) degrees, there are \(.) solutions.") ;
 
task1([90, 60, 120]), "", task2(60; 10000)</lang>
{{out}}
<pre>
For sides in the range [1, 13] where they can all be of the same length:
 
For an angle of 90 degrees, there are 3 solutions, namely:
[[3,4,5],[5,12,13],[6,8,10]]
For an angle of 60 degrees, there are 15 solutions, namely:
[[1,1,1],[2,2,2],[3,3,3],[3,8,7],[4,4,4],[5,5,5],[5,8,7],[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10],[11,11,11],[12,12,12],[13,13,13]]
For an angle of 120 degrees, there are 2 solutions, namely:
[[3,5,7],[7,8,13]]
 
For sides in the range [1, 10000] where they cannot ALL be of the same length:
For an angle of 60 degrees, there are 18394 solutions.
</pre>
 
 
=={{header|Julia}}==
2,482

edits