public void SetData() { primes = new Primes((long)Math.Pow(maxSize, 2)); triples = new HashSet <PythagoreanTriple>(new PythComp()); // https://www.chilimath.com/lessons/geometry-lessons/generating-pythagorean-triples/ long a, b; // Generate all triples where short leg < 100, and long leg (which will become sum of other two legs) < 2*MaxSize for (long n = 1; 2 * n <= (maxSize * 2); n++) { for (long m = n + 1; Math.Pow(m, 2) - Math.Pow(n, 2) <= maxSize; m++) { a = (int)(Math.Pow(m, 2) - Math.Pow(n, 2)); b = 2 * n * m; if (a > (maxSize * 2) || b > (maxSize * 2)) { break; } if (primes.Reduce(ref a, ref b)) { continue; } if (!triples.Add(new PythagoreanTriple(a, b))) { throw new Exception("Attempted to add duplicate."); } ; } } }