Example #1
0
        public void Problem41()
        {
            var primesUnder1B = new SortedSet <long>(
                Sequences
                .PrimesUnder(1_000_000)
                .ToList()
                );

            var sum = new BigInteger(0);

            for (int i = 1; i <= 9; i++)
            {
                sum += Functions.Permutations(i, i);
            }
            Console.WriteLine(sum);

            for (int n = 9; n >= 4; n--)
            {
                // arithmetic series is going to be the sum of the series 1 to n.
                // If the sum of the series 1 to n is divisible by 3, all
                // permutations of 1 to n will be composites.
                if (Series.Arithemtic(n) % 3 != 0)
                {
                    var permutationsOf = Sequences.PermutationsOf(n, 0, 1);
                    foreach (var permutation in permutationsOf)
                    {
                        if (permutation % 2 != 0 && Functions.IsPrime(permutation))
                        {
                            Assert.That(permutation, Is.EqualTo(7_652_413));
                            return;
                        }
                    }
                }
            }
        }
Example #2
0
        public void Problem43()
        {
            // This is pretty slow.
            Func <long, bool> meetsProperty = (permutation) =>
                                              ((permutation % 1_000_000_000) / 1_000_000) % 2 == 0 &&
                                              ((permutation % 100_000_000) / 100_000) % 3 == 0 &&
                                              ((permutation % 10_000_000) / 10_000) % 5 == 0 &&
                                              ((permutation % 1_000_000) / 1_000) % 7 == 0 &&
                                              ((permutation % 100_000) / 100) % 11 == 0 &&
                                              ((permutation % 10_000) / 10) % 13 == 0 &&
                                              ((permutation % 1_000)) % 17 == 0;

            var sum = Sequences.PermutationsOf(10, 1, 1)
                      .Where(meetsProperty)
                      .Sum();

            Console.WriteLine(sum);
        }
Example #3
0
        public void Problem32()
        {
            var permutations = Sequences.PermutationsOf(9, 0, 1)
                               .ToList();

            SortedSet <long> products = new SortedSet <long>();
            long             sum      = 0;

            foreach (var permutation in permutations)
            {
                {
                    var left    = permutation / 10_000_000;
                    var right   = (permutation % 10_000_000) / 10_000;
                    var product = permutation % 10_000;

                    if (left * right == product)
                    {
                        if (!products.Contains(product))
                        {
                            products.Add(product);
                        }
                    }
                }


                {
                    var left    = permutation / 100_000_000;
                    var right   = (permutation % 100_000_000) / 10_000;
                    var product = permutation % 10_000;

                    if (left * right == product)
                    {
                        if (!products.Contains(product))
                        {
                            products.Add(product);
                        }
                    }
                }
            }
            Assert.That(products.Sum(), Is.EqualTo(45_228));
        }