Jump to content

Law of cosines - triples: Difference between revisions

Added 11l
m (→‎{{header|Raku}}: remove un-needed '*.')
(Added 11l)
Line 35:
* [https://youtu.be/p-0SOWbzUYI?t=12m11s Visualising Pythagoras: ultimate proofs and crazy contortions] Mathlogger Video
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>-V n = 13
 
F method1(n)
V squares = (0..n).map(x -> x ^ 2)
V sqrset = Set(squares)
Set[(Int, Int, Int)] tri90, tri60, tri120
 
L(a) 1..n
V a2 = squares[a]
L(b) 1..a
V b2 = squares[b]
V c2 = a2 + b2
I c2 C sqrset
tri90.add(tuple_sorted((a, b, Int(sqrt(c2)))))
V ab = a * b
c2 -= ab
I c2 C sqrset
tri60.add(tuple_sorted((a, b, Int(sqrt(c2)))))
c2 += 2 * ab
I c2 C sqrset
tri120.add(tuple_sorted((a, b, Int(sqrt(c2)))))
R [sorted(Array(tri90)),
sorted(Array(tri60)),
sorted(Array(tri120))]
 
print(‘Integer triangular triples for sides 1..#.:’.format(n))
L(angle, triples) zip([90, 60, 120], method1(n))
print(" #3° has #. solutions:\n #.".format(angle, triples.len, triples))
V t60 = method1(10'000)[1]
V notsame = sum(t60.filter((a, b, c) -> a != b | b != c).map((a, b, c) -> 1))
print(‘Extra credit: ’notsame)</lang>
 
{{out}}
<pre>
Integer triangular triples for sides 1..13:
90° has 3 solutions:
[(3, 4, 5), (5, 12, 13), (6, 8, 10)]
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)]
120° has 2 solutions:
[(3, 5, 7), (7, 8, 13)]
Extra credit: 18394
</pre>
 
=={{header|Ada}}==
1,481

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.