示例#1
0
        public ActionResult Problem35(ulong n)
        {
            var cal = new PrimeCalculator((uint)n / 3);

            cal.ExtendToMinimumGT(n);

            Func <ulong, bool> isCircular = (i) =>
            {
                ulong c         = i;
                uint  numDigits = Utils.NumOfDigits(c);
                ulong factor    = Utils.PowerOfTen(numDigits - 1);
                do
                {
                    ulong lower  = (c / 10);
                    ulong higher = (c % 10) * factor;   // Move rightmost digit to leftmost position
                    c = higher + lower;

                    if (!cal.IsPrimeInRange(c))
                    {
                        return(false);
                    }
                }while (c != i);

                return(true);
            };

            int cnt = cal.Primes.Count(isCircular);

            return(ViewAnswer(35, "The number of circular primes below " + n + " is", (uint)cnt));
        }