private static int PandigitalHigherPrime(int missing, int number)
        {
            if (missing == 0)
            {
                if (PrimeNumbers.IsPrimeNumber(number))
                {
                    return(number);
                }

                return(-1);
            }

            var aux    = missing;
            var div    = 10;
            var antDiv = 1;

            while (aux > 0)
            {
                var nextMissing = missing / div * antDiv + missing % antDiv;
                var nextNumber  = number * 10 + aux % 10;
                var result      = PandigitalHigherPrime(nextMissing, nextNumber);

                if (result > -1)
                {
                    return(result);
                }

                aux   /= 10;
                antDiv = div;
                div   *= 10;
            }

            return(-1);
        }
예제 #2
0
        public static int ProductOfCoefficientOfQuadraticFormulaProducingMorePrimeNumbers()
        {
            var maxPrimeNumbers = int.MinValue;
            int aValue          = int.MinValue;
            int bValue          = int.MinValue;
            var primesUntil999  = PrimeNumbers.PrimesUntilN(999);

            for (int a = -999; a < 1000; a++)
            {
                for (int b = 2; b <= 999; b++)
                {
                    if (primesUntil999[b])
                    {
                        int n;

                        for (n = 1; PrimeNumbers.IsPrimeNumber(n * n + a * n + b); n++)
                        {
                            ;
                        }

                        if (n > maxPrimeNumbers)
                        {
                            maxPrimeNumbers = n;
                            aValue          = a;
                            bValue          = b;
                        }
                    }
                }
            }

            return(aValue * bValue);
        }
예제 #3
0
        public void MenuDisplay()
        {
            string menuInput;

            do
            {
                Console.Clear();
                Console.WriteLine("================================================");
                Console.WriteLine("Which C# skills exercise would you like to try?");
                Console.WriteLine("Sum of given number.                         (1)");
                Console.WriteLine("Sum of 3 and 5 multiples for a given number. (2)");
                Console.WriteLine("Generate product of a given number.          (3)");
                Console.WriteLine("Multiplication table.                        (4)");
                Console.WriteLine("List a whole lotta prime numbers.            (5)");

                /*Console.WriteLine("                                (6)");
                 * Console.WriteLine();
                 * Console.WriteLine();
                 * Console.WriteLine();*/
                Console.WriteLine("================================================");
                Console.WriteLine();
                Console.Write("Enter choice:");
                menuInput = Console.ReadLine();

                switch (menuInput)
                {
                case "1":
                    PrintSum newSum = new PrintSum();
                    newSum.RequestInput();
                    break;

                case "2":
                    SumThreeAndFive tfsum = new SumThreeAndFive();
                    tfsum.ThreeFiveInput();
                    break;

                case "3":
                    Product gen = new Product();
                    gen.FindProduct();
                    break;

                case "4":
                    TimesTables mult = new TimesTables();
                    mult.Incrementor();
                    break;

                case "5":
                    PrimeNumbers prime = new PrimeNumbers();
                    prime.CalcPrime();
                    break;

                case "6":

                    break;
                }
            } while (true);
        }
        public static long PrimePermutations()
        {
            var primesWith4Numbers = new List <(long num, int orderedNum)>();

            for (int i = 1235; i <= 9875; i += 2)
            {
                if (PrimeNumbers.IsPrimeNumber(i))
                {
                    primesWith4Numbers.Add((i, OrderedNum(i)));
                }
            }

            for (int i = 0; i < primesWith4Numbers.Count; i++)
            {
                var(firstNum, firstOrderedNum) = primesWith4Numbers[i];

                for (int j = i + 1; j < primesWith4Numbers.Count; j++)
                {
                    if (firstOrderedNum == primesWith4Numbers[j].orderedNum)
                    {
                        var(secondNum, secondOrderedNum) = primesWith4Numbers[j];
                        var firstDiff = secondNum - firstNum;

                        for (int k = j + 1; k < primesWith4Numbers.Count; k++)
                        {
                            var(thirdNum, thirdOrderedNum) = primesWith4Numbers[k];
                            var lastDiff = thirdNum - secondNum;

                            if (lastDiff > firstDiff)
                            {
                                break;
                            }

                            if (lastDiff == firstDiff && thirdOrderedNum == firstOrderedNum)
                            {
                                var joinedNumbers = firstNum.JoinWith(secondNum.JoinWith(thirdNum));

                                if (joinedNumbers != 148748178147)
                                {
                                    return(joinedNumbers);
                                }
                            }
                        }
                    }
                }
            }

            return(0);
        }
예제 #5
0
        public static long SumPrimesBelowN(int num)
        {
            var  primes = PrimeNumbers.PrimesUntilN(num - 1);
            long sum    = 2;

            for (int i = 3; i < primes.Length; i += 2)
            {
                if (primes[i])
                {
                    sum += i;
                }
            }

            return(sum);
        }
예제 #6
0
        public static int ConsecutivePrimeSum(int num)
        {
            var primes           = PrimeNumbers.PrimesUntilN(num);
            int greatestPrimeSum = 0;
            int largestStreak    = 0;

            for (int i = 0; i < primes.Length; i++)
            {
                if (primes[i])
                {
                    int currentStreak = 0;
                    int currentSum    = 0;

                    for (int j = i; j < primes.Length; j++)
                    {
                        if (primes[j])
                        {
                            currentSum += j;

                            if (currentSum >= num)
                            {
                                break;
                            }

                            currentStreak++;

                            if (primes[currentSum] && currentStreak > largestStreak)
                            {
                                largestStreak    = currentStreak;
                                greatestPrimeSum = currentSum;
                            }
                        }
                    }
                }
            }

            return(greatestPrimeSum);
        }
 public E035_Circular_primes(int threshold)
 {
     _threshold    = threshold;
     _primeNumbers = PrimeNumbers.PrimesUntilN(_threshold - 1);
 }