using System; using System.Collections.Generic; namespace A338890 { /// /// A338892 Primitive terms of A338890. /// class Program { /// /// Compute first terms of sequence A338892. /// static void Main() { List primitive = new List(); for (long hypothenuse = 1; hypothenuse < 1000000; hypothenuse++) { long squareOfHypothenuse = hypothenuse * hypothenuse; Squares[squareOfHypothenuse] = hypothenuse; PythagoreanTriangles[hypothenuse] = new List(); long squareOfLeftLeg; for (long leftLeg = 1; 2*(squareOfLeftLeg=leftLeg*leftLeg)<=squareOfHypothenuse; leftLeg++) { long squareOfRightLeg = squareOfHypothenuse - squareOfLeftLeg; long rightLeg; if (Squares.TryGetValue(squareOfRightLeg, out rightLeg)) { PythagoreanTriangle triangle = new PythagoreanTriangle() { Hypothenuse = hypothenuse, LeftLeg = leftLeg, RightLeg = rightLeg, }; PythagoreanTriangles[hypothenuse].Add(triangle); PythagoreanTriangles[hypothenuse].Add(triangle.Flip); } } if (IsOK(hypothenuse)) { bool keep = true; foreach (long p in primitive) { if (hypothenuse%p==0) { keep = false; break; } } if (keep) { primitive.Add(hypothenuse); Console.WriteLine($"{primitive.Count} {hypothenuse}"); } } } } /// /// Is hypothenuse OK? /// /// hypothenuse of triangle /// OK? static bool IsOK(long hypothenuse) { foreach (PythagoreanTriangle triangle in PythagoreanTriangles[hypothenuse]) { foreach (PythagoreanTriangle leftTriangle in PythagoreanTriangles[triangle.LeftLeg]) { foreach (PythagoreanTriangle rightTriangle in PythagoreanTriangles[triangle.RightLeg]) { if (leftTriangle.RightLeg == rightTriangle.LeftLeg) { return true; } } } } return false; } /// /// Square table (key=n^2, value=n). /// static readonly Dictionary Squares = new Dictionary(); /// /// List of pythagorean triangles indexed by hypothenuse. /// static readonly Dictionary> PythagoreanTriangles = new Dictionary>(); } /// /// Pythagorean triangle (Top^2 = Left^2 + Right^2). /// public class PythagoreanTriangle { public long Hypothenuse { get; set; } public long LeftLeg { get; set; } public long RightLeg { get; set; } /// /// Triangle with bottom values flipped. /// public PythagoreanTriangle Flip { get { return new PythagoreanTriangle() { Hypothenuse = this.Hypothenuse, LeftLeg = this.RightLeg, RightLeg = this.LeftLeg, }; } } } }