Ejemplo n.º 1
0
 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
 }
Ejemplo n.º 2
0
 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();
 }
Ejemplo n.º 3
0
 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);
 }
Ejemplo n.º 4
0
 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();
 }