public static Object Solve()
 {
     InfiniteIntList.StartIndex = -999;
     return(InfiniteIntList.Items.TakeWhile(a => a <= 999)
            .SelectMany(a => InfiniteIntList.Items.TakeWhile(b => b <= 999)
                        .Select(b => new { a, b }))
            .OrderByDescending(p => InfiniteIntList.GetItems(0).TakeWhile(n => (n * (p.a + n) + p.b).IsPrime())
                               .LastOrDefault())
            .Select(p => p.a * p.b)
            .First());
 }
Exemple #2
0
 public static Object Solve()
 {
     InfiniteIntList.StartIndex = SmallestPerimeter;
     return(InfiniteIntList.Items
            .TakeWhile(p => p <= 1000)
            .SelectMany(p => InfiniteIntList.GetItems(MinSideLength)
                        .TakeWhile(a => a <= MaxSideLength)
                        .SelectMany(a => InfiniteIntList.GetItems(MinSideLength)
                                    .Skip((int)a)
                                    .TakeWhile(b => b < (p - a) / 2)
                                    .Select(b => new { p, a, b, c = p - b - a })))
            .Where(x => Math.Pow(x.a, 2) + Math.Pow(x.b, 2) == Math.Pow(x.c, 2))
            // Filter out duplicate entries
            .GroupBy(x => x.p)
            .OrderByDescending(group => group.Count())
            .First()
            .Key);
 }
Exemple #3
0
        public static Object Solve()
        {
            // Build matrix in memory array
            var matrix = matrixString.Split(' ').Select(x => Int64.Parse(x));

            var solution = InfiniteIntList.GetItems(0, gridSize)     // horizontal
                           .Take(gridSize)
                           .SelectMany(
                r => InfiniteIntList.GetItems(r, 1)
                .Take(gridSize - lineSize + 1)
                .Select(
                    p => matrix.Skip((int)p)
                    .Take(4)
                    .Product()
                    )
                )
                           .Concat(
                InfiniteIntList.GetItems(0, gridSize)                                         // diagonal (left to right)
                .Take(gridSize - lineSize + 1)
                .SelectMany(
                    r => InfiniteIntList.GetItems(r, 1)
                    .Take(gridSize - lineSize + 1)
                    .Select(
                        p => InfiniteIntList.GetItems(p, gridSize + 1)
                        .Take(4)
                        .Select(x => matrix.ElementAt((int)x))
                        .Product()
                        )
                    )

                )
                           .Concat(
                InfiniteIntList.GetItems(0, gridSize)                                         // vertical
                .Take(gridSize - lineSize + 1)
                .SelectMany(
                    r => InfiniteIntList.GetItems(r, 1)
                    .Take(gridSize)
                    .Select(
                        p => InfiniteIntList.GetItems(p, gridSize)
                        .Take(4)
                        .Select(x => matrix.ElementAt((int)x))
                        .Product()
                        )
                    )
                )
                           .Concat(
                InfiniteIntList.GetItems(0, gridSize)                                        // diagonal (right to left)
                .Take(gridSize - lineSize + 1)
                .SelectMany(
                    r => InfiniteIntList.GetItems(r + lineSize - 1, 1)
                    .Take(gridSize - lineSize + 1)
                    .Select(
                        p => InfiniteIntList.GetItems(p, gridSize - 1)
                        .Take(4)
                        .Select(x => matrix.ElementAt((int)x))
                        .Product()
                        )
                    )
                )
                           .Max();

            return(solution);
        }