예제 #1
0
        public List <string> Solve(List <Argument> input)
        {
            List <string> results = new List <string>();
            int           limit;
            BigInteger    result    = 0;
            Sequences     sequences = new Sequences();

            if (int.TryParse(input.First().Value.ToString(), out limit))
            {
                BigInteger fib = 1;
                for (int i = 1; fib <= limit; i++)
                {
                    if (fib % 2 == 0)
                    {
                        result += fib;
                    }

                    fib = sequences.GetFibonacciTerm(i);
                }
            }

            results.Add(result.ToString());

            return(results);
        }
예제 #2
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;
                        }
                    }
                }
            }
        }
예제 #3
0
        public void Problem7()
        {
            var answer
                = Sequences.PrimeNumbers()
                  .Skip(10_000)
                  .First();

            Assert.That(answer, Is.EqualTo(104743));
        }
예제 #4
0
        public void Problem21()
        {
            Func <int, dynamic> getDivisorsInfo = n =>
            {
                var uniqueDivisors = Sequences.UniqueDivisors(n).ToList();
                return(new
                {
                    Number = n,
                    AmicableSum = uniqueDivisors.Sum() - n
                });
            };

            var divisors_cache = Enumerable.Range(1, 9_999)
                                 .Select(getDivisorsInfo)
                                 .ToList();

            var d_cache = divisors_cache
                          .ToDictionary(x => x.Number, x => x.AmicableSum);

            Func <int, int> d = (n) =>
            {
                if (d_cache.ContainsKey(n))
                {
                    return(d_cache[n]);
                }
                else
                {
                    var divisorsInfo = getDivisorsInfo(n);

                    divisors_cache.Add(divisorsInfo);
                    d_cache.Add(divisorsInfo.Number, divisorsInfo.AmicableSum);

                    return(divisorsInfo.AmicableSum);
                }
            };

            var amicableNumbersUnder10000 = Enumerable.Range(1, 9_999)
                                            .Where(a =>
            {
                var b = d(a);
                return(a == d(b) && a != b);
            })
                                            .ToList();

            var answer
                = amicableNumbersUnder10000.Sum();


            Assert.That(answer, Is.EqualTo(31_626));
        }
예제 #5
0
        public void Problem10()
        {
            long sum = 0;

            foreach (var prime in Sequences.PrimeNumbers())
            {
                if (prime >= 2_000_000)
                {
                    break;
                }
                sum += prime;
            }

            long answer = sum;

            Assert.That(answer, Is.EqualTo(142_913_828_922));
        }
예제 #6
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);
        }
예제 #7
0
        public void Problem25()
        {
            var sequence = Sequences.Fibonacci(new BigInteger(1), new BigInteger(1));

            var index = 0;

            foreach (var number in sequence)
            {
                index++;

                if (number.ToString().Length == 1000)
                {
                    break;
                }
            }

            Assert.That(index, Is.EqualTo(4_782));
        }
예제 #8
0
        public void Problem2()
        {
            long sum = 0;

            foreach (long term in Sequences.Fibonacci(1, 2))
            {
                if (term >= 4_000_000)
                {
                    break;
                }

                Console.WriteLine(term);
                if (term % 2 == 0)
                {
                    sum += term;
                }
            }

            Console.WriteLine(sum);
        }
예제 #9
0
        public void Problem23()
        {
            var abundantNumbers = new SortedSet <int>();

            for (int n = 12; n <= 28_123; n++)
            {
                var uniqueDivisors = Sequences.UniqueDivisors(n);
                var sum            = uniqueDivisors.Sum() - n;

                if (sum > n)
                {
                    abundantNumbers.Add(n);
                }
            }

            var numbersThatAreNotSumOfTwoAbundantNumbers = new SortedSet <int>(
                Enumerable.Range(1, 28_123)
                .Select(x => x)
                .ToList()
                );

            var abundantNumberList = abundantNumbers.ToList();

            for (int i = 0; i < abundantNumberList.Count; i++)
            {
                var iNum = abundantNumberList[i];
                for (int j = i; j < abundantNumberList.Count; j++)
                {
                    var jNum = abundantNumberList[j];

                    var sum = iNum + jNum;

                    if (numbersThatAreNotSumOfTwoAbundantNumbers.Contains(sum))
                    {
                        numbersThatAreNotSumOfTwoAbundantNumbers.Remove(sum);
                    }
                }
            }

            Assert.That(numbersThatAreNotSumOfTwoAbundantNumbers.Sum(), Is.EqualTo(4_179_871));
        }
예제 #10
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));
        }