예제 #1
0
        private string TryToFindSolution(int numOfDigits, int desiredFamilySize)
        {
            List <int>    primes    = Primality.GetPrimeListWithMaxValue((int)Math.Pow(10, numOfDigits));
            List <string> primesStr = primes.Select(i => i.ToString()).ToList();

            string bestSoFar = "z";

            List <string> relevantPrimeStr = primesStr.Where(s => s.Length == numOfDigits).ToList();

            for (int obscureNum = 1; obscureNum < Math.Pow(2, numOfDigits) - 1; obscureNum++)
            {
                BitVector32 bv        = new BitVector32(obscureNum);
                var         groupings = relevantPrimeStr.GroupBy(s => ObscureChars(s, obscureNum)).Where(x => x.Key != "");
                var         good      = groupings.Where(x => x.Count() >= desiredFamilySize).ToList();
                if (good.Any())
                {
                    string possibleBest = good.Min(g => g.Min());
                    if (possibleBest.CompareTo(bestSoFar) < 0)
                    {
                        bestSoFar = possibleBest;
                    }
                }
            }
            if (bestSoFar != "z")
            {
                return(bestSoFar);
            }

            return("");
        }
예제 #2
0
        public override string GetAnswer()
        {
            int second, third;

            List <int>      primes         = Primality.GetPrimeListWithMaxValue(10000);
            SortedSet <int> unsortedPrimes = new SortedSet <int>(primes);

            foreach (int first in primes.Where(i => i < 5000))
            {
                for (int addAmt = 2; addAmt < 4500; addAmt++)
                {
                    second = first + addAmt;
                    if (unsortedPrimes.Contains(second))
                    {
                        third = second + addAmt;
                        if (unsortedPrimes.Contains(third))
                        {
                            if (areThreeNumsPermutations(first, second, third))
                            {
                                if (first != 1487)
                                {
                                    return(first.ToString() + second.ToString() + third.ToString());
                                }
                            }
                        }
                    }
                }
            }
            throw new Exception("Answer Not Found?!?!");
        }
예제 #3
0
        public override string GetAnswer()
        {
            // n^2 + a*n + b
            // b MUST be prime (because for n=0, b's the only term used)
            //      We'll assume b is odd (why it can't be 2 or -2 later on)
            // n^2 + a*n MUST generate sequences that are always even
            //    if it toggles back and forth even/odd, then the terms generated will be even/odd
            //    (hint: there aren't many prime numbers that are even, so the #-in-a-row will be a bit constrained.)
            // n^2 toggles back and forth even/odd, so a*n must do so as well (so that the two, added together, are always even.)
            // aka, a must be odd.
            // (b can't be even, since the equation either toggles even/odd (bad), is always even (b=even,a=odd), or always odd (a,b=odd)

            int bestA = 0; int bestB = 0; int bestVal = 0;


            for (int b = -999; b < 1000; b += 2)
            {
                if (Primality.isPrime(Math.Abs(b)))
                {
                    for (int a = -999; a < 1000; a += 2)
                    {
                        int testVal = GetNumberOfSequentialPrimes(a, b);
                        if (testVal >= bestVal)
                        {
                            bestA   = a;
                            bestB   = b;
                            bestVal = testVal;
                        }
                    }
                }
            }
            return((bestA * bestB).ToString());
        }
예제 #4
0
        public override string GetAnswer()
        {
            int primesFound    = 3;
            int nonPrimesFound = 2;

            int sideLen = 3;

            int[] candidates = new int[3];
            while (primesFound * 9 > nonPrimesFound)
            {
                sideLen += 2;

                nonPrimesFound++; // bottom right diagonal will always be non-prime: it's a perfect square.

                int totalWithSideLen = sideLen * sideLen;
                candidates[0] = totalWithSideLen - sideLen + 1;
                candidates[1] = candidates[0] - sideLen + 1;
                candidates[2] = candidates[1] - sideLen + 1;

                foreach (int candidate in candidates)
                {
                    if (Primality.isPrime(candidate))
                    {
                        primesFound++;
                    }
                    else
                    {
                        nonPrimesFound++;
                    }
                }
            }

            return(sideLen.ToString());
        }
예제 #5
0
        private int checkDigitCount(int digitCount)
        {
            int largestPrime = 0;
            Func <List <int>, int> DigitListToNumFunc = l => l.Aggregate((a, b) => a * 10 + b);

            List <int> digits = new List <int>();

            for (int i = 1; i <= digitCount; i++)
            {
                digits.Add(i);
            }

            IEnumerable <List <int> > allCombos = Shared.GetAllPermutations(digits);

            foreach (List <int> permut in allCombos)
            {
                int actualNum = DigitListToNumFunc(permut);
                if (Primality.isPrime(actualNum))
                {
                    if (actualNum > largestPrime)
                    {
                        largestPrime = actualNum;
                    }
                }
            }
            return(largestPrime);
        }
예제 #6
0
        public override string GetAnswer()
        {
            List <int> millionPrimes = Primality.GetPrimeListWithMaxValue(1000000);

            PossibleAnswer bestAnswerSoFar = GetBestAnswerForStartSlot(millionPrimes, 0);

            int totalSlotsAvail = millionPrimes.Count;

            for (int i = 1; i < totalSlotsAvail; i++)
            {
                int relevantPrime = millionPrimes[i];
                int playspace     = 1000000 - bestAnswerSoFar.finalSum;
                if (relevantPrime * bestAnswerSoFar.numberOfPrimes * 2 > playspace)
                {
                    return(bestAnswerSoFar.ToString());
                }
                if (relevantPrime * bestAnswerSoFar.numberOfPrimes > 1000000)
                {
                    return(bestAnswerSoFar.ToString());
                }
                PossibleAnswer bestAnswerForSlot = GetBestAnswerForStartSlot(millionPrimes, i);
                if (bestAnswerForSlot.numberOfPrimes > bestAnswerSoFar.numberOfPrimes)
                {
                    bestAnswerSoFar = bestAnswerForSlot;
                }
            }

            return(millionPrimes.Count.ToString());
        }
예제 #7
0
        private bool DoTwoPrimesWork(int primeA, int primeB)
        {
            int shrinkA = primeA;
            int shrinkB = primeB;
            int expandA = 1;
            int expandB = 1;

            while (shrinkA > 0)
            {
                shrinkA = shrinkA / 10;
                expandA = expandA * 10;
            }

            int testA = primeA + primeB * expandA;

            if (!Primality.isPrime(testA))
            {
                return(false);
            }
            while (shrinkB > 0)
            {
                shrinkB = shrinkB / 10;
                expandB = expandB * 10;
            }
            int testB = primeB + primeA * expandB;

            if (!Primality.isPrime(testB))
            {
                return(false);
            }
            return(true);
        }
예제 #8
0
        public override string GetAnswer()
        {
            List <int> primesList = Primality.GetPrimeListWithMaxValue(10000);  // note: tested with larger values to make sure


            SortedSet <pair> reflectivePairs = new SortedSet <pair>();

            foreach (int x in primesList)
            {
                foreach (int y in primesList)
                {
                    if (x < y)
                    {
                        if (DoTwoPrimesWork(x, y))
                        {
                            reflectivePairs.Add(new pair(x, y));
                        }
                    }
                }
            }


            var chainOfThree  = reflectivePairs.Join(reflectivePairs, a => a.x, b => b.x, (c, d) => new { c, d }).Where(g => g.c.y < g.d.y).ToList();
            var circleOfThree = chainOfThree.Where(g => reflectivePairs.Contains(new pair(g.c.y, g.d.y))).Select(g => new { x = g.c.x, y = g.c.y, z = g.d.y }).ToList();

            var chainOfFour  = circleOfThree.Join(circleOfThree, a => new { a.x, a.y }, b => new { b.x, b.y }, (c, d) => new { c, d }).Where(g => g.c.z < g.d.z).ToList();
            var circleOfFour = chainOfFour.Where(g => reflectivePairs.Contains(new pair(g.c.z, g.d.z))).Select(g => new { x = g.c.x, y = g.c.y, z = g.c.z, w = g.d.z }).ToList();

            var chainOfFive  = circleOfFour.Join(circleOfFour, a => new { a.x, a.y, a.z }, b => new { b.x, b.y, b.z }, (c, d) => new { c, d }).Where(g => g.c.w < g.d.w).ToList();
            var circleOfFive = chainOfFive.Where(g => reflectivePairs.Contains(new pair(g.c.w, g.d.w))).Select(g => new { x = g.c.x, y = g.c.y, z = g.c.z, w = g.c.w, v = g.d.w }).ToList();



            return(circleOfFive.Min(g => g.x + g.y + g.z + g.w + g.v).ToString());
        }
예제 #9
0
        public void GeneralTestTest()
        {
            Primality p = new Primality();

            Assert.AreEqual(p.GeneralTest(3599), false);
            Assert.AreEqual(p.GeneralTest(113), true);
            Assert.AreEqual(p.GeneralTest(4), false);
        }
예제 #10
0
        private List <ulong> GetNextStageRightRemovable(List <ulong> prevStage)
        {
            List <ulong> retVal = new List <ulong>();

            for (ulong rightDigit = 1; rightDigit <= 9; rightDigit++)
            {
                retVal.AddRange(prevStage.Where(n => Primality.isPrime(n * 10 + rightDigit)).Select(n => n * 10 + rightDigit));
            }
            return(retVal);
        }
예제 #11
0
        private static void DoTest(StreamReader sr)
        {
            int p = Convert.ToInt32(sr.ReadLine());

            for (int tItr = 0; tItr < p; tItr++)
            {
                int n = Convert.ToInt32(sr.ReadLine());
                Console.WriteLine(Primality.primality(n));
            }
        }
예제 #12
0
        private List <ulong> GetNextStageLeftRemovable(List <ulong> prevStage, ulong ScalarOfLeftDigit)
        {
            List <ulong> retVal = new List <ulong>();

            for (ulong leftDigit = 1; leftDigit <= 9; leftDigit++)
            {
                ulong amtToAdd = leftDigit * ScalarOfLeftDigit;
                retVal.AddRange(prevStage.Where(n => Primality.isPrime(amtToAdd + n)).Select(n => n + amtToAdd));
            }
            return(retVal);
        }
예제 #13
0
        private int GetNumberOfSequentialPrimes(int a, int b)
        {
            int n = 0;

            while (true)
            {
                int x = n * n + a * n + b;
                x = Math.Abs(x);
                if (!Primality.isPrime(x))
                {
                    return(n);
                }
                n++;
            }
        }
예제 #14
0
        public override string GetAnswer()
        {
            List <int> circular   = new List <int>();
            List <int> candidates = new List <int> {
                2, 3, 5, 7
            };

            for (int i = 11; i <= 999999; i += 2)
            {
                string testStr = i.ToString();
                if (!testStr.Contains("0"))
                {
                    if (!testStr.Contains("2"))
                    {
                        if (!testStr.Contains("4"))
                        {
                            if (!testStr.Contains("5"))
                            {
                                if (!testStr.Contains("6"))
                                {
                                    if (!testStr.Contains("8"))
                                    {
                                        candidates.Add(i);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            while (candidates.FirstOrDefault() != 0)
            {
                int        trialNum = candidates.First();
                List <int> allRots  = GetAllRotations(trialNum);
                if (allRots.All(n => Primality.isPrime(n)))
                {
                    circular.AddRange(allRots);
                }
                allRots.ForEach(x => candidates.Remove(x));
            }


            return(circular.Count().ToString());
        }
예제 #15
0
        public override string GetAnswer()
        {
            List <ulong> answers = new List <ulong>();

            List <ulong> leftRemovable  = Primality.GetPrimeListWithMaxValue(9).Select(n => (ulong)n).ToList();
            List <ulong> rightRemovable = Primality.GetPrimeListWithMaxValue(9).Select(n => (ulong)n).ToList();
            ulong        curScalar      = 10;

            while (answers.Count() < 11)
            {
                leftRemovable  = GetNextStageLeftRemovable(leftRemovable, curScalar);
                rightRemovable = GetNextStageRightRemovable(rightRemovable);
                curScalar      = curScalar * 10;

                answers.AddRange(leftRemovable.Where(n => rightRemovable.Contains(n)));
            }

            ulong finalAnswer = answers.Aggregate((a, b) => a + b);

            return(finalAnswer.ToString());
        }
예제 #16
0
        public override string GetAnswer()
        {
            // okay, so here's our approach:
            // get a list of all the odd composite numbers below a certain threshhold
            // then, we copy this list into a possi

            int             upperThreshhold = 1000000;
            SortedSet <int> oddComposites   = new SortedSet <int>();

            for (int i = 9; i < upperThreshhold; i += 2)
            {
                if (!Primality.isPrime(i))
                {
                    oddComposites.Add(i);
                }
            }

            List <int> possibilities = new List <int>();

            possibilities.AddRange(oddComposites);

            int subtractNum = 0;
            Func <int, bool> IsStillOkay = (i) => oddComposites.Contains(i - subtractNum) || (i - subtractNum < 1);

            int loopThresh = (int)Math.Sqrt(upperThreshhold / 2 + 1);

            for (int sq = 1; sq <= loopThresh; sq++)
            {
                subtractNum   = 2 * sq * sq;
                possibilities = possibilities.Where(i => IsStillOkay(i)).ToList();
            }
            //subtractNum = 8;
            //possibilities = possibilities.Where(i => IsStillOkay(i)).ToList();


            return(possibilities.Min().ToString());
        }
예제 #17
0
        public void FermatTestTest()
        {
            Primality p = new Primality();

            Assert.AreEqual(p.FermatTest(3599), false);
        }