public static long SimpleCrossSelect(long sum) { return(CrossSelect( // generate tuples where Item1 + Item2 + Item3 == 1000 ClosedRange(1, sum), ClosedRange(1, sum), (a, b) => (a: a, b: b, c: sum - a - b) ) .Where(tup => tup.c > 0) .Where(tup => tup.b >= tup.a) .Where(tup => tup.a.Squared() + tup.b.Squared() == tup.c.Squared()) // keep only valid pythagorean triples .Select(tup => tup.a * tup.b * tup.c) // get product of tuple .First()); // get first product }
static void Main(string[] args) { CrossSelect( // generate tuples where Item1 + Item2 + Item3 == 1000 ClosedRange(1, 1000), ClosedRange(1, 1000), (a, b) => (a: a, b: b, c: 1000 - a - b) ) .Where(tup => tup.c > 0) .Where(tup => tup.b >= tup.a) .Where(tup => tup.a.Squared() + tup.b.Squared() == tup.c.Squared()) // keep only valid pythagorean triples .Select(tup => tup.a * tup.b * tup.c) // get product of tuple .First() // get first product .ConsoleWriteLine(); }
public static long SimpleCrossSelect(long max) { return(CrossSelect( ClosedRange(1, max), ClosedRange(1, max), (l, r) => (a: l, b: r, c: Math.Sqrt(l.Squared() + r.Squared())) ) .Where(pt => pt.a + pt.b + pt.c < max) .Where(pt => pt.c % 1 == 0) .Select(pt => pt.a + pt.b + (long)(pt.c)) .GroupBy(p => p) .OrderByDescending(g => g.Count()) .First() .Key); }
static void Main(string[] args) { CrossSelect( ClosedRange(1, 1000), ClosedRange(1, 1000), (l, r) => (a: l, b: r, c: Math.Sqrt(l.Squared() + r.Squared())) ) .Where(pt => pt.a + pt.b + pt.c < 1000) .Where(pt => pt.c % 1 == 0) .Select(pt => pt.a + pt.b + (long)(pt.c)) .GroupBy(p => p) .OrderByDescending(g => g.Count()) .First() .Key .ConsoleWriteLine(); }