Exemplo n.º 1
0
        private IEnumerable <SideLengthAndRatio> SideLengthAndRatios()
        {
            var primeCalculate = new Primes(1000000000);

            primeCalculate.CalculatePrimes();
            var primes          = primeCalculate.PrimeList().GetEnumerator();
            var numberDiagonals = 1;
            var numberPrimes    = 0;

            for (long length = 3;; length += 2)
            {
                var corner = length * length;
                var step   = length - 1;

                // create small prime checking set
                var primeSet = new HashSet <long>();
                while (primes.MoveNext())
                {
                    var next = primes.Current;
                    if (next > corner)
                    {
                        break;
                    }
                    primeSet.Add(next);
                }

                // exclude corner as it is clearly not prime
                numberPrimes += new[] { corner - step, corner - step * 2, corner - step * 3 }
                .Count(x => primeSet.Contains(x));
                numberDiagonals += 4;
                yield return(new SideLengthAndRatio(length, numberPrimes, numberDiagonals));
            }
        }
Exemplo n.º 2
0
        public string Solve()
        {
            var p = new Primes(2000000);

            p.CalculatePrimes();
            long s = p.PrimeList().Select(x => (long)x).Sum();

            return(s.ToString());
        }
Exemplo n.º 3
0
        public IEnumerable <int> BruteSolve()
        {
            var primeCalculator = new Primes(1000000);
            var primes          = new HashSet <int>(primeCalculator.PrimeList());

            return(primes
                   .Where(IsTruncatableFirstPass)
                   .Where(x => Truncations(x).All(y => primes.Contains(y))));
        }
Exemplo n.º 4
0
        private IEnumerable <int> PrimesUnder(int n)
        {
            // add more primes if needed
            if (_primesList.LastOrDefault() < n)
            {
                checked
                {
                    _primes     = new Primes(n * n > 100000 ? n * n : 100000);
                    _primesList = _primes.PrimeList().ToList();
                }
            }

            var i = GetIndexOfLessThanOrEqual(_primesList, n);

            return(_primesList.Take(i + 1));
        }
Exemplo n.º 5
0
        public string Solve()
        {
            var primes = new Primes((int)1e9);

            primes.CalculatePrimes();

            foreach (var prime in primes.PrimesListBackwards())
            {
                if (IsPandigital(prime))
                {
                    return(prime.ToString());
                }
            }

            return("");
        }
Exemplo n.º 6
0
        public Tuple <int, int, int> FindPrimeFormula()
        {
            // one of the largest possible value is 1000*1000 + 1000*1000 + 1000 < 2100000
            // with the restrictions given
            var primes    = new Primes(2100000);
            var primesSet = new HashSet <int>(primes.PrimeList());

            var abCombinations = from i in Enumerable.Range(-1000, 2000)
                                 from j in Enumerable.Range(-1000, 2000)
                                 select Tuple.Create(i, j);

            var answer = abCombinations.Select(n =>
            {
                var count = QuadraticWith(n.Item1, n.Item2).TakeWhile(primesSet.Contains).Count();
                return(Tuple.Create(n.Item1, n.Item2, count));
            }).Aggregate((a, b) => a.Item3 > b.Item3 ? a : b);

            return(answer);
        }
Exemplo n.º 7
0
        public static int GetEnglishNumberLetterCount(int n)
        {
            // pad the number to be divisible by three for grouping convenience
            var radix  = n.ToString();
            var addPad = Primes.Mod(-radix.Length, 3);

            radix = radix.PadLeft(radix.Length + addPad);

            var count = 0;

            foreach (var t in radix.SplitUpIntoGroups(3).Enumerate())
            {
                var i       = t.Item1;
                var grp     = t.Item2;
                var numbers = grp.Aggregate("", (prev, next) => prev + next);
                count += CountEnglishNumberGroup(Convert.ToInt32(numbers));
                count += ThreeDigitSectionNames[(int)i];
            }
            return(count);
        }
Exemplo n.º 8
0
        private IEnumerable <int> BruteSolve()
        {
            var primesCalculator = new Primes(1000000);
            var primes           = new HashSet <int>(primesCalculator.PrimeList());

            var circularPrimes = new HashSet <int>();

            foreach (var prime in primes)
            {
                if (circularPrimes.Contains(prime))
                {
                    continue;
                }

                var rotations = Rotations(prime);
                if (rotations.All(x => primes.Contains(x)))
                {
                    rotations.ForEach(rotation => circularPrimes.Add(rotation));
                }
            }

            return(circularPrimes);
        }
Exemplo n.º 9
0
        public string Solve()
        {
            var primesList = new Primes(1000000).PrimeList().ToArray();
            var primes     = new HashSet <int>(primesList);

            // brute force: check every combination of consecutive primes
            var largestStart = 0;
            var largestCount = 0;
            var largestPrime = 0;

            for (var i = 1; i < primesList.Length; i++)
            {
                var sum = primesList[i];
                for (var j = i + 1; j < primesList.Length; j++)
                {
                    sum += primesList[j];
                    if (sum > 1000000)
                    {
                        break;
                    }

                    if (j - i > largestCount && primes.Contains(sum))
                    {
                        largestStart = i;
                        largestCount = j - i;
                        largestPrime = sum;
                    }
                }

                if (sum > 1000000)
                {
                    continue;
                }
            }

            return(largestPrime.ToString());
        }