Beispiel #1
0
 public override long Solve()
 {
     for (int i = 9; i > 0; i--)
     {
         var pandigitals = Pandigitals.Generate(i).OrderByDescending(p => p);
         foreach (var pandigital in pandigitals)
         {
             if (Primes.IsPrime(pandigital))
             {
                 return(pandigital);
             }
         }
     }
     return(0);
 }
Beispiel #2
0
        //12.5 seconds without filtering out invalid pandigitals.
        //filtering inputs:
        //Filter out pandigitals where d6 is not divisble by 5: 11.4 seconds
        public override long Solve()
        {
            var primes      = Primes.CalculatePrimesBelow(18).ToArray();
            var pandigitals = Pandigitals.GenerateZeroToN(9);

            long sum = 0;

            foreach (var pandigital in pandigitals)
            {
                if (PossibleSubstringDivisibleByPrime(pandigital))
                {
                    var divisibleByPrime = true;
                    for (int i = 0; i < primes.Length && divisibleByPrime; i++)
                    {
                        divisibleByPrime = SubstringDivisibleBy(pandigital, i + 1, primes[i]);
                    }
                    if (divisibleByPrime)
                    {
                        sum += pandigital;
                    }
                }
            }
            return(sum);
        }