Example #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));
            }
        }
Example #2
0
        public string Solve()
        {
            var p = new Primes(2000000);

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

            return(s.ToString());
        }
Example #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))));
        }
Example #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));
        }
Example #5
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);
        }
Example #6
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);
        }