//public static bool isPrime(int number)
        //{
        //    // if we've previously examined this in the generator, might as well use that data!
        //    if (number <= PrimeGenerator.largestKnownOrderedPrime)
        //        return PrimeGenerator.IsKnownPrime(number);


        //    if (number == 1) return false;
        //    if (number == 2) return true;

        //    if (number % 2 == 0) return false;

        //    int boundary = (int)Math.Sqrt(number);

        //    for (int i = 3; i <= boundary; i += 2)
        //    {
        //        if (number % i == 0) return false;
        //    }
        //    return true;
        //}

        //public static bool isPrime(ulong number)
        //{
        //    // if we've previously examined this in the generator, might as well use that data!
        //    if (number <= (ulong)PrimeGenerator.largestKnownOrderedPrime)
        //        return PrimeGenerator.IsKnownPrime((int)number);

        //    if (number == 1) return false;
        //    if (number == 2) return true;

        //    if (number % 2 == 0) return false;

        //    ulong boundary = (ulong)Math.Sqrt(number);

        //    for (ulong i = 3; i <= boundary; i += 2)
        //    {
        //        if (number % i == 0) return false;
        //    }
        //    return true;
        //}

        private static bool isPrime(dynamic number)
        {
            // if we've previously examined this in the generator, might as well use that data!
            if (number <= PrimeGenerator.largestKnownOrderedPrime)
            {
                return(PrimeGenerator.IsKnownPrime((int)number));
            }

            if (number == 1)
            {
                return(false);
            }
            if (number == 2)
            {
                return(true);
            }

            if (number % 2 == 0)
            {
                return(false);
            }

            ulong boundary = (ulong)Math.Sqrt(number);

            for (ulong i = 3; i <= boundary; i += 2)
            {
                if (number % i == 0)
                {
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
        public override void Process()
        {
            PrimeGenerator primeGenerator = new PrimeGenerator();
            int upperLimit = 1000000, chainLength = 0, maxPrime = 0;

            List<int> primes = primeGenerator.Below(upperLimit);
            long[] consecutiveSum = GenerateConsecutiveSum(primes);

            for (int j = chainLength; j < consecutiveSum.Count(); j++)
            {
                for (int k = j - (chainLength + 1); k >= 0; k--)
                {
                    int checkPrime = (int)(consecutiveSum[j] - consecutiveSum[k]);
                    if (checkPrime > upperLimit)
                        break;

                    if (!primeGenerator.IsPrime(checkPrime))
                        continue;

                    chainLength = j - k;
                    maxPrime = checkPrime;
                }
            }

            Console.WriteLine("Prime: {0} made from a chain of {1} consecutive primes", maxPrime, chainLength);
        }
Example #3
0
        private int GetAnswerForLargestDivisor(int largestDivisor)
        {
            List <int> allPrimesBelow20 = PrimeGenerator.GetPrimeListWithMaxValue(largestDivisor);
            List <int> primesToLargestPowerStillBelow20 = allPrimesBelow20.Select(
                a =>
                (int)Math.Pow(a,
                              (int)(Math.Log(largestDivisor) / Math.Log(a))
                              )).ToList();
            int smallestAnswer = primesToLargestPowerStillBelow20.Aggregate((a, b) => a * b);

            return(smallestAnswer);
        }
Example #4
0
        public override void Process()
        {
            int upperLimit = 10000;

            PrimeGenerator generator = new PrimeGenerator();
            List<int> primes = generator.Between(1000, upperLimit);

            for (int i = 0; i < primes.Count(); i++)
            {
                int a = primes[i];
                for (int j = i + 1; j < primes.Count(); j++)
                {
                    int b = primes[j];
                    int c = b + (b - a);
                    if (c > upperLimit)
                        break;

                    if (generator.IsPrime(c) && ArePermutable(a, b, c))
                    {
                        Console.WriteLine("{0}{1}{2} (Step: {3})", a, b, c, c - b);
                    }
                }
            }
        }
Example #5
0
        private int GetNthPrime(int term)
        {
            List <int> primes = PrimeGenerator.GetPrimeListWithNTerms(term);

            return(primes.Max());
        }
Example #6
0
        private long GetAnswerWithMaxPrime(int maxPrime)
        {
            List <int> primes = PrimeGenerator.GetPrimeListWithMaxValue(maxPrime);

            return(primes.Sum(a => (long)a));
        }