コード例 #1
0
ファイル: Sequences.cs プロジェクト: ajdust/euler
 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));
 }
コード例 #2
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());
        }
コード例 #3
0
ファイル: Problem047.cs プロジェクト: ajdust/euler
        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());
        }
コード例 #4
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)
                       ));
        }
コード例 #5
0
ファイル: NumberExtensions.cs プロジェクト: ajdust/euler
 public static BigInteger Factorial(this long n)
 {
     return(n <= 1 ? 1 : Sequences.LongRange(1, n).Aggregate(new BigInteger(1), (acc, x) => acc * x));
 }