예제 #1
0
        public int ConsecutivePrimeSum(int max)
        {
            var primes = PrimeNumberHelper.GetPrimeNumbersBelowNumber(max);

            var allSums = new List <List <Tuple <int, int> > >();
            int i       = 0;

            while (i < primes.Count)
            {
                var sums  = new List <Tuple <int, int> >();
                var next  = 0;
                var terms = 0;
                foreach (var prime in primes.Skip(i))
                {
                    terms++;
                    next += prime;
                    if (next > max)
                    {
                        allSums.Add(sums);
                        break;
                    }
                    sums.Add(new Tuple <int, int>(terms, next));
                }
                i++;
            }
            var combinedSums = allSums.SelectMany(x => x).OrderByDescending(x => x.Item1).ToList();

            foreach (var sum in combinedSums)
            {
                if (primes.Contains(sum.Item2))
                {
                    return(sum.Item2);
                }
            }
            return(-1);
        }
예제 #2
0
 public double GetSumOfPrimesBelowNumber(int number)
 {
     return(PrimeNumberHelper.GetPrimeNumbersBelowNumber(number).Aggregate <int, double>(0, (current, i) => current + i));
 }