public int Problem9() { int s = 1000; int mlimit = (int)Math.Sqrt(s / 2); var range = Enumerable.Range(2, mlimit - 2); Func <int, Tuple <bool, int> > action = m => { if ((s / 2) % m == 0) { int k = m % 2 == 0 ? m + 1 : m + 2; while (k < 2 * m && k <= s / (2 * m)) { if (s / (2 * m) % k == 0 && MathsHelper.gcd(k, m) == 1) { int n = k - m; int d = s / (2 * m * k); return(Tuple.Create(true, MathsHelper.Power(d, 3) * ((m * m) - (n * n)) * (2 * m * n) * ((m * m) + (n * n)))); } k += 2; } } return(Tuple.Create(false, 0)); }; return(range.Select(action).First(x => x.Item1).Item2); }
private static IEnumerable <int> GetPerimetersOfPrimitivePythagoreanTriplets(int s) { //generate coprime m,n with exactly one m,n even int m = 2; int n = 1; int mlimit = (int)Math.Sqrt(s / 2); while (m <= mlimit) { n = m % 2 == 0 ? 1 : 2; while (n < m) { if (MathsHelper.gcd(m, n) == 1) { yield return(2 * m * (m + n)); } n += 2; } m++; } }