Beispiel #1
0
        public static long ConsecutivePrimeSum()
        {
            long consecutivePrime = 0;

            List <long> primesList = PrimeHelpers.ExecuteSieveofEratosthenes(1000000);

            long currentPrime = primesList[primesList.Count - 1];
            long maxVal       = 0;

            for (var j = (primesList.Count - 1) - 1; j >= 0; j--)
            {
                long currentSum = 0;

                for (var k = j; k >= 0 && currentSum <= currentPrime; k--)
                {
                    currentSum += primesList[k];
                    if (j - k > maxVal)
                    {
                        maxVal = j - k;
                        if (primesList.Contains(currentSum))
                        {
                            consecutivePrime = currentSum;
                        }
                    }
                }
            }

            return(consecutivePrime);
        }
Beispiel #2
0
        public static int DistinctPrimesFactors()
        {
            int number     = 2 * 3 * 5 * 7;
            int foundCount = 0;

            List <long> primeList = PrimeHelpers.ExecuteSieveofEratosthenes(1000000);

            while (foundCount < 4)
            {
                long tempNumber       = number;
                long prime            = primeList[0];
                int  primeCount       = 0;
                bool isDifferentPrime = false;
                int  primeIndex       = 0;

                primeCount = tempNumber % prime == 0 ? 1 : 0;

                while (tempNumber > 1)
                {
                    if (tempNumber % prime == 0)
                    {
                        tempNumber /= prime;
                        if (isDifferentPrime)
                        {
                            primeCount++;
                            isDifferentPrime = false;
                        }
                    }
                    else
                    {
                        primeIndex++;
                        prime            = primeList[primeIndex];
                        isDifferentPrime = true;
                    }
                }

                number++;
                if (tempNumber == 1 && primeCount == 4)
                {
                    foundCount++;
                }
                else
                {
                    foundCount = 0;
                }
            }

            return(number - 4);
        }
Beispiel #3
0
        public static long PandigitalPrime()
        {
            long result    = 0;
            var  primeList = PrimeHelpers.ExecuteSieveofEratosthenes(987654321);

            for (var i = primeList.Count - 1; i >= 0; i--)
            {
                if (MathHelpers.IsPandigital(primeList[i]))
                {
                    result = primeList[i];
                    break;
                }
            }

            return(result);
        }