Esempio n. 1
0
        public ActionResult Problem41(uint n)
        {
            if (n > 9)
            {
                n = 9;
            }

            // Set up prime calculator
            var cal = new PrimeCalculator();

            // Keep trying until we run out of digits.
            while (n > 0)
            {
                List <int> digits       = Enumerable.Range(1, (int)n).ToList();
                var        permutations = new Permutations <int>(digits);

                var allPrimes = permutations.Select(numList => numList.MakeNumber())
                                .Where(num => cal.IsPrimeAutoExpand(num)).ToList();

                // If there is at least 1 prime with n-digit, find the maximum amongst these primes and
                // that's the answer
                if (allPrimes.Count > 0)
                {
                    ulong max = allPrimes.Max();
                    return(ViewAnswer(41, "The maximum " + n + "-digit pandigital prime is", max));
                }

                // Otherwise, reduce number of digits and try again.
                --n;
            }

            return(ViewAnswer(41, "No n-digit pandigital prime found.", 0));
        }
Esempio n. 2
0
        public ActionResult Problem27(uint n)
        {
            ulong max = 2 * n * n + n;  // because n^2 + n*n + n is divisible by n
            var   cal = new PrimeCalculator((uint)max / 3);

            int max_a = 0, max_b = 0, max_i = 0;
            int minus_n = (-1 * (int)n) + 1;

            for (int a = minus_n; a < n; a++)
            {
                for (int b = minus_n; b < n; b++)
                {
                    int i;
                    for (i = 0; i < n; i++)
                    {
                        long p = i * i + a * i + b;
                        if (p <= 0)
                        {
                            break;
                        }

                        if (!cal.IsPrimeAutoExpand((ulong)p))
                        {
                            break;
                        }
                    }

                    if (i > max_i)
                    {
                        max_i = i;
                        max_a = a;
                        max_b = b;
                    }
                }
            }

            string s = string.Format("a = {0} b = {1} n = 0..{2} a*b = ", max_a, max_b, max_i);

            return(ViewAnswer(27, s, max_a * max_b));
        }
Esempio n. 3
0
        public ActionResult Problem50(ulong n)
        {
            var cal = new PrimeCalculator(40000);

            cal.ExtendToMinimumGT(n);

            // Cumsums.. cumsum[i] = sum of primes[0 .. i]
            var primes  = cal.Primes;
            var cumsums = primes.CumSum().ToArray();

            // Calc sum from [fromPos, toPos]
            Func <int, int, ulong> calcSum = (fromPos, toPos) =>
            {
                int before = fromPos - 1;
                if (before >= 0)
                {
                    return(cumsums[toPos] - cumsums[before]);
                }
                else
                {
                    return(cumsums[toPos]);
                }
            };

            // Find end position for range to examine.
            int endPos;

            for (endPos = primes.Length - 1; endPos >= 0 && primes[endPos] > n; endPos--)
            {
                ;
            }

            // Loop through all possible values for [start, end] to find the longest range whose
            // sum is still a prime
            int   maxLen = 0, maxStart = 0;
            ulong maxNum = 0;

            for (int end = 0; end <= endPos; end++)
            {
                for (int start = 0; start < end; start++)
                {
                    int len = end - start + 1;
                    if (len < maxLen)
                    {
                        continue;
                    }

                    ulong sum = calcSum(start, end);
                    if (sum > n)
                    {
                        continue;
                    }

                    if (!cal.IsPrimeAutoExpand(sum))
                    {
                        continue;
                    }

                    maxLen   = len;
                    maxNum   = sum;
                    maxStart = start;
                }
            }

            int   maxEnd   = maxStart + maxLen - 1;
            ulong maxValue = calcSum(maxStart, maxEnd);
            var   s        = string.Format("{0} + .. + {1} = {2} len = {3}",
                                           primes[maxStart], primes[maxEnd], maxValue, maxEnd, maxLen);

            return(ViewAnswer(50, s, maxValue));
        }