Пример #1
0
        public long Problem3()
        {
            long        number = 600851475143;
            List <long> primes = new List <long>();

            for (long i = 2; i <= Math.Pow(number, 0.5); i++)
            {
                if (number % i == 0)
                {
                    long otherDivisor = number / i;
                    if (MathsHelper.IsPrime(i))
                    {
                        primes.Add(i);
                    }
                    else
                    {
                        if (MathsHelper.IsPrime(otherDivisor))
                        {
                            return(otherDivisor);
                        }
                    }
                }
            }

            primes.Reverse();

            return(primes[0]);
        }
Пример #2
0
        public void Problem27()
        {
            Dictionary <long, bool> isPrimeDict = new Dictionary <long, bool>();
            long maxConsecutive = 0;
            Tuple <long, long> coeffs;

            for (long a = -500; a < 500; a++)
            {
                long aValue = (2 * a) + 1;
                for (long b = -500; b < 500; b++)
                {
                    long bValue = (2 * b) + 1;
                    //bool resultPrime = true;
                    while (true)
                    {
                        long quadValue = this.GetQuadraticValue(aValue, bValue, 0);
                        if (!isPrimeDict.ContainsKey(quadValue))
                        {
                            bool isPrime = MathsHelper.IsPrime(quadValue);
                            isPrimeDict.Add(quadValue, isPrime);
                        }
                        if (isPrimeDict[quadValue])
                        {
                            maxConsecutive++;
                            coeffs = Tuple.Create(aValue, bValue);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
Пример #3
0
        public int TheNthPrimeIs(int whichNumerPrime)
        {
            int i            = 1;
            int numberToTest = 3;

            while (i < whichNumerPrime)
            {
                if (MathsHelper.IsPrime(numberToTest))
                {
                    i++;
                }
                numberToTest++;
            }
            return(numberToTest - 1);
        }
Пример #4
0
        public int Problem37()
        {
            var truncatable = new Dictionary <int, IEnumerable <IEnumerable <int> > >();
            IEnumerable <IEnumerable <int> > choices = new int[][] { new int[] { 1 }, new int[] { 3 }, new int[] { 7 }, new int[] { 9 } };
            var tailElements = new HashSet <int>(new int[] { 3, 7 });
            //var primes = new HashSet<int>(GetPrimesUpTo(1000000000));

            //primes.

            Func <IEnumerable <int>, int> getNumber = list => list.Reverse().Select((i, j) => i * MathsHelper.Power(10, j)).Sum();
            Func <IEnumerable <int>, IEnumerable <IEnumerable <int> > > getRLSubsets = list => list.Select((i, j) => list.Skip(j));

            truncatable.Add(1, new int[][] { new int[] { 2 }, new int[] { 3 }, new int[] { 5 }, new int[] { 7 } });


            for (int q = 2; q < 20; q++)
            {
                truncatable[q]     = truncatable[q - 1].SelectMany(p => choices, (r, t) => r.Concat(t));
                truncatable[q]     = truncatable[q].Where(x => MathsHelper.IsPrime((long)getNumber(x))).ToList();
                truncatable[q - 1] = truncatable[q - 1].Where(x => getRLSubsets(x).Select(getNumber).All(y => MathsHelper.IsPrime((long)y)));
                if (truncatable[q - 1].Count() == 0)
                {
                    truncatable[q] = new List <IEnumerable <int> >();
                    break;
                }
            }

            var tyy = truncatable.Where(x => x.Key >= 2).Select(x => x.Value).Select(x => x.Select(getNumber)).SelectMany(x => x).Sum();


            IEnumerable <int> test = Enumerable.Range(1, 4);
            var hg = test.Select((i, j) => test.Skip(j));



            return(0);
        }