Exemple #1
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);
        }
        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);
        }
        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);
        }