Law of cosines - triples: Difference between revisions
Content added Content deleted
(Added Prolog Solution) |
(Added C#) |
||
Line 482: | Line 482: | ||
There are 18394 solutions for gamma = 60 degrees in the range 1 to 10000 where the sides are not all of the same length. |
There are 18394 solutions for gamma = 60 degrees in the range 1 to 10000 where the sides are not all of the same length. |
||
</pre> |
</pre> |
||
=={{header|C sharp}}== |
|||
<lang csharp>using System; |
|||
using System.Collections.Generic; |
|||
using static System.Linq.Enumerable; |
|||
public static class LawOfCosinesTriples |
|||
{ |
|||
public static void Main2() { |
|||
PrintTriples(60, 13); |
|||
PrintTriples(90, 13); |
|||
PrintTriples(120, 13); |
|||
PrintTriples(60, 10_000, true, false); |
|||
} |
|||
private static void PrintTriples(int degrees, int maxSideLength, bool notAllTheSameLength = false, bool print = true) { |
|||
string s = $"{degrees} degree triangles in range 1..{maxSideLength}"; |
|||
if (notAllTheSameLength) s += " where not all sides are the same"; |
|||
Console.WriteLine(s); |
|||
int count = 0; |
|||
var triples = FindTriples(degrees, maxSideLength); |
|||
if (notAllTheSameLength) triples = triples.Where(NotAllTheSameLength); |
|||
foreach (var triple in triples) { |
|||
count++; |
|||
if (print) Console.WriteLine(triple); |
|||
} |
|||
Console.WriteLine($"{count} solutions"); |
|||
} |
|||
private static IEnumerable<(int a, int b, int c)> FindTriples(int degrees, int maxSideLength) { |
|||
double radians = degrees * Math.PI / 180; |
|||
int coefficient = (int)Math.Round(Math.Cos(radians) * -2, MidpointRounding.AwayFromZero); |
|||
int maxSideLengthSquared = maxSideLength * maxSideLength; |
|||
return |
|||
from a in Range(1, maxSideLength) |
|||
from b in Range(1, a) |
|||
let cc = a * a + b * b + a * b * coefficient |
|||
where cc <= maxSideLengthSquared |
|||
let c = (int)Math.Sqrt(cc) |
|||
where c * c == cc |
|||
select (a, b, c); |
|||
} |
|||
private static bool NotAllTheSameLength((int a, int b, int c) triple) => triple.a != triple.b || triple.a != triple.c; |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
60 degree triangles in range 1..13 |
|||
(1, 1, 1) |
|||
(2, 2, 2) |
|||
(3, 3, 3) |
|||
(4, 4, 4) |
|||
(5, 5, 5) |
|||
(6, 6, 6) |
|||
(7, 7, 7) |
|||
(8, 3, 7) |
|||
(8, 5, 7) |
|||
(8, 8, 8) |
|||
(9, 9, 9) |
|||
(10, 10, 10) |
|||
(11, 11, 11) |
|||
(12, 12, 12) |
|||
(13, 13, 13) |
|||
15 solutions |
|||
90 degree triangles in range 1..13 |
|||
(4, 3, 5) |
|||
(8, 6, 10) |
|||
(12, 5, 13) |
|||
3 solutions |
|||
120 degree triangles in range 1..13 |
|||
(5, 3, 7) |
|||
(8, 7, 13) |
|||
2 solutions |
|||
60 degree triangles in range 1..10000 where not all sides are the same |
|||
18394 solutions</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |