Exemplo n.º 1
0
 static long Solve(int n)
 {
     // finding the nth prime, reusing Problem 3's prime enumerable,
     // which while not the most efficient prime finding algorithm, works reasonably
     // well for finding only the prime number 10001
     return(Sequences.Primes().Skip(n).First());
 }
Exemplo n.º 2
0
 public string Solve()
 {
     return(Enumerable.Range(2, 999998)
            .Select(n => new Tuple <long, long>(n, Sequences.Collatz(n).Count()))
            .Aggregate(new Tuple <long, long>(0, 0), (prev, next) => next.Item2 > prev.Item2 ? next : prev)
            .Item1.ToString());
 }
Exemplo n.º 3
0
        public static HashSet <long> GetFactors(long n)
        {
            if (FactorsDictionary.ContainsKey(n))
            {
                return(FactorsDictionary[n]);
            }

            // initialize the factor list to the prime factors and 1 and n
            var primeFactors = Sequences.GetPrimeFactors(n);
            var factorSet    = new HashSet <long>(primeFactors)
            {
                1, n
            };

            // divide by primes and recursively determine the factors to be aggregated
            foreach (var prime in primeFactors)
            {
                var factor = n / prime;
                if (factor <= 2 || factorSet.Contains(factor))
                {
                    continue;
                }
                foreach (var subfactor in GetFactors(factor))
                {
                    factorSet.Add(subfactor);
                }
            }

            FactorsDictionary[n] = factorSet;
            return(factorSet);
        }
Exemplo n.º 4
0
        public string Solve()
        {
            var abundantSums = new HashSet <long>(Sequences.GetAbundantSums(28123).ToList());
            var answer       = Enumerable.Range(1, 28123).Where(n => !abundantSums.Contains(n)).Sum();

            return(answer.ToString());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Find the prime factors of a^b.
        /// </summary>
        public static IEnumerable <long> PowerFactors(int a, int b)
        {
            var baseFactors = Sequences.GetPrimeFactors(a);
            var allFactors  = Enumerable.Range(0, b).SelectMany(n => baseFactors);

            return(from factor in allFactors orderby factor select factor);
        }
Exemplo n.º 6
0
 public static IEnumerable <long> GetAbundantNumbers(long limit)
 {
     return
         (Sequences.LongRange(1, limit)
          .Select(n => new Tuple <long, HashSet <long> >(n, Problem012.GetFactors(n)))
          .Where(t => t.Item1 < t.Item2.Sum() - t.Item1)    // subtract t.Item1 to get only proper divisors
          .Select(t => t.Item1));
 }
Exemplo n.º 7
0
        public string Solve()
        {
            var words          = GetWords();
            var wordValues     = words.Select(GetWordValue).ToArray();
            var maxValue       = wordValues.Max();
            var triangleValues = new HashSet <long>(Sequences.TriangleNumbers().TakeWhile(x => x < maxValue));

            return(wordValues.Count(x => triangleValues.Contains(x)).ToString());
        }
Exemplo n.º 8
0
        private int BruteSolve()
        {
            var maxPerimeter = Sequences.PythagoreanTriplets()
                               .TakeWhile(x => x.Item1 + x.Item2 + x.Item3 < 1000)
                               .ToLookup(x => x.Item1 + x.Item2 + x.Item3)
                               .Aggregate(Tuple.Create(0, 0), (maxes, perimeters) =>
                                          perimeters.Count() > maxes.Item1 ? Tuple.Create(perimeters.Count(), perimeters.Key) : maxes);

            return(maxPerimeter.Item2);
        }
Exemplo n.º 9
0
        public string Solve()
        {
            var count = Sequences
                        .LongRange(1, 100)
                        .Select(n => Sequences
                                .LongRange(2, 100)
                                .Count(r => nCr(n, r) > 1000000))
                        .Sum();

            return(count.ToString());
        }
Exemplo n.º 10
0
        public string Solve()
        {
            var answer = Sequences.LongRange(1, int.MaxValue)
                         .First(n =>
            {
                var count = new[] { n, n + 1, n + 2, n + 3 }
                .TakeWhile(x => Sequences.GetPrimeFactors(x).Distinct().Count() == 4)
                .Count();
                return(count == 4);
            });

            return(answer.ToString());
        }
Exemplo n.º 11
0
 static Tuple <int, int, int> SolveAbcAddTo1000()
 {
     foreach (var triplet in Sequences.PythagoreanTriplets())
     {
         var a = triplet.Item1;
         var b = triplet.Item2;
         var c = triplet.Item3;
         if (a + b + c == 1000)
         {
             return(new Tuple <int, int, int>(a, b, c));
         }
         if (a + b + c > 2000)
         {
             break;
         }
     }
     return(null);
 }
Exemplo n.º 12
0
        public static long GetGreatestCommonFactor(long a, long b)
        {
            var aPrimeFactors = Sequences.GetPrimeFactors(a).GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
            var bPrimeFactors = Sequences.GetPrimeFactors(b).GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

            // collect the factors between the two, similar to Problem005,
            // except this time pool the shared minimums
            var minCount = new Dictionary <long, int>();

            foreach (var factor in aPrimeFactors.Concat(bPrimeFactors))
            {
                if (aPrimeFactors.ContainsKey(factor.Key) && bPrimeFactors.ContainsKey(factor.Key))
                {
                    minCount[factor.Key] = minCount.ContainsKey(factor.Key)
                      ? Math.Min(minCount[factor.Key], factor.Value)
                      : factor.Value;
                }
            }

            return(minCount.Aggregate <KeyValuePair <long, int>, long>(
                       1, (current, factor) => current * (long)Math.Pow(factor.Key, factor.Value)));
        }
Exemplo n.º 13
0
        static long CommonFactorCountSolve()
        {
            // find the common factors between the numbers 1 to 20
            var factorCounts = Sequences.LongRange(1, 20).Select(n => Sequences.GetPrimeFactors(n).GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count()));

            // collect the maximum factor counts for each of the numbers;
            // for instance, 16 is 2^4, so the 2 factors of 20 (2*2*5)
            // are not necessary as they are already accounted for
            var maxCount = new Dictionary <long, int>();

            foreach (var factor in factorCounts.SelectMany(factorCount => factorCount))
            {
                maxCount[factor.Key] = maxCount.ContainsKey(factor.Key)
                    ? Math.Max(maxCount[factor.Key], factor.Value)
                    : factor.Value;
            }

            // compute the smallest number that is divisible by all numbers from 1 to 20,
            // ie, the number with the minimum number of factors shared by those numbers
            return(maxCount.Aggregate <KeyValuePair <long, int>, long>(
                       1, (current, factor) => current * (long)Math.Pow(factor.Key, factor.Value)
                       ));
        }
Exemplo n.º 14
0
 public string Solve()
 {
     return(Sequences.TriangleNumbers().First(t => GetFactors(t).Count > 500).ToString());
 }
Exemplo n.º 15
0
        public string Solve()
        {
            var answer = Sequences.BigFibonacci().Enumerate().First(t => t.Item2 > BigInteger.Pow(10, 999));

            return(answer.Item1.ToString());
        }
Exemplo n.º 16
0
 static int Solve(int maxFibValue)
 {
     return(Sequences.Fibonacci().Where(x => x % 2 == 0).TakeWhile(x => (x < maxFibValue)).Sum());
 }
Exemplo n.º 17
0
 public static BigInteger Factorial(this long n)
 {
     return(n <= 1 ? 1 : Sequences.LongRange(1, n).Aggregate(new BigInteger(1), (acc, x) => acc * x));
 }
Exemplo n.º 18
0
 public string Solve()
 {
     return(Sequences.DateTimeRange("1901-01-01T12:00:00", "2000-12-31T12:00:00", 60 * 60 * 24)
            .Count(n => n.Day == 1 && n.DayOfWeek == DayOfWeek.Sunday).ToString());
 }