Exemple #1
0
        public object Solve()
        {
            // b must be a prime since a^2 + an + b = b when n = 0
            _sieve = new PrimeSieve(10000);

            var  greatestCount = 0;
            long aGreat = 0, bGreat = 0;

            for (var a = -999; a < 1000; a++)
            {
                foreach (var b in _sieve.GetPrimes().TakeWhile(p => p < 1000))
                {
                    var count = GetConsecutivePrimeCount(a, b);

                    if (count > greatestCount)
                    {
                        greatestCount = count;
                        aGreat        = a;
                        bGreat        = b;
                    }
                }
            }

            return(aGreat * bGreat);
        }
Exemple #2
0
        public void Providing()
        {
            var sieve = new PrimeSieve(1000);

            var primes = sieve.GetPrimes().ToArray();

            primes.Length.ShouldBe(168);
            primes[0].ShouldBe(2);
            primes[1].ShouldBe(3);
            primes[2].ShouldBe(5);
        }
Exemple #3
0
        private long LookForSolution(int limit, int mod)
        {
            var primes = _sieve.GetPrimes().Where(p => p % 3 == mod && p <= limit).ToArray();

            _buddyLookup = CreateBuddyLookup(primes);

            foreach (var rootPrime in _buddyLookup)
            {
                foreach (var friend in rootPrime.Value)
                {
                    var intersection = rootPrime.Value.Intersect(_buddyLookup[friend]).ToArray();
                    if (intersection.Length < 3)
                    {
                        continue;
                    }

                    long result = 0;

                    intersection.IjFor((i, j) =>
                    {
                        var p1 = intersection[i];
                        var p2 = intersection[j];
                        if (!AreBuddies(p1, p2))
                        {
                            return;
                        }

                        for (var k = j + 1; k < intersection.Length; k++)
                        {
                            var p3 = intersection[k];

                            if (AreBuddies(p1, p3) && AreBuddies(p2, p3))
                            {
                                result = rootPrime.Key + friend + p1 + p2 + p3;
                            }
                        }
                    });

                    if (result > 0)
                    {
                        return(result);
                    }
                }
            }

            _buddyLookup = null;

            return(0);
        }
Exemple #4
0
        public object Solve()
        {
            const int arbitraryLimit = 200000;
            var       sieve          = new PrimeSieve(arbitraryLimit);

            var numPrimeFactors = new int[arbitraryLimit];

            foreach (var prime in sieve.GetPrimes())
            {
                for (var number = prime; number < arbitraryLimit; number += prime)
                {
                    numPrimeFactors[number]++;
                }
            }

            return(FindTheSmallestOfFourConsecutive(arbitraryLimit, number => numPrimeFactors[number]));
        }
Exemple #5
0
        public object Solve()
        {
            // Easily done by hand, but generalized to be fast for numbers as large as 10^5
            // (Fun fact: The product at this magnitude is an insane 43452 digits)
            var number = 20;

            var sieve = new PrimeSieve(number);

            BigInteger product = 1;

            foreach (var prime in sieve.GetPrimes())
            {
                product *= GetLargestPowerOfPrime(prime, number);
            }

            return(product);
        }
Exemple #6
0
        public object Solve()
        {
            var limit = 5 * 10.Power(7);

            var primeLimit = limit.Sqrt().Ceiling().ToLong();

            var sieve = new PrimeSieve(primeLimit);

            var squareRootPrimes = sieve.GetPrimes().ToArray();
            var cubeRootPrimes   = squareRootPrimes.Where(p => p <= limit.NthRoot(3)).ToArray();
            var fourthRootPrimes = squareRootPrimes.Where(p => p <= limit.NthRoot(4)).ToArray();

            var numbers = new HashSet <long>();

            foreach (var p1 in squareRootPrimes)
            {
                foreach (var p2 in cubeRootPrimes)
                {
                    var partialSum = p1.Power(2) + p2.Power(3);

                    if (partialSum > limit)
                    {
                        break;
                    }

                    foreach (var p3 in fourthRootPrimes)
                    {
                        var sum = partialSum + p3.Power(4);

                        if (sum < limit && !numbers.Contains(sum))
                        {
                            numbers.Add(sum);
                        }
                    }
                }
            }

            return(numbers.Count);
        }
Exemple #7
0
        public object Solve()
        {
            var sieve = new PrimeSieve(Limit);

            return(sieve.GetPrimes().Sum());
        }