Ejemplo n.º 1
0
        public static long Problem37()
        {
            /*
             * The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
             *
             * Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
             *
             * NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
             */
            long res = 0L;

            List <long> primes            = Formulas.PrimeSieve(1000000);
            List <long> truncatablePrimes = new List <long>();

            int i = 8;
            int j = 0;

            while (j < 11)
            {
                if (i > primes.Last())
                {
                    return(-2);
                }

                if (Formulas.isTruncatable(i))
                {
                    j++;
                    res += i;
                }

                i++;
            }

            return(res);
        }
Ejemplo n.º 2
0
        public static long Problem43()
        {
            /*
             * The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
             *
             * Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:
             *
             * d2d3d4=406 is divisible by 2
             * d3d4d5=063 is divisible by 3
             * d4d5d6=635 is divisible by 5
             * d5d6d7=357 is divisible by 7
             * d6d7d8=572 is divisible by 11
             * d7d8d9=728 is divisible by 13
             * d8d9d10=289 is divisible by 17
             * Find the sum of all 0 to 9 pandigital numbers with this property.
             */

            List <string> numbers = Formulas.PandigitalNumbers(0, 9);
            List <long>   primes  = Formulas.PrimeSieve(20);

            long result = 0L;

            foreach (string str in numbers)
            {
                bool buf = true;

                for (int i = 1; i <= 7; i++)
                {
                    if (Int64.Parse(String.Concat(String.Concat(str[i], str[i + 1]), str[i + 2])) % primes.ElementAt(i - 1) != 0)
                    {
                        buf = false;
                    }
                }

                if (buf)
                {
                    result += Int64.Parse(str);
                }
            }



            return(result);
        }
Ejemplo n.º 3
0
        public static long Problem49()
        {
            /*
             * The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
             * There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
             * What 12-digit number do you form by concatenating the three terms in this sequence?
             */

            List <long>   primes  = Formulas.PrimeSieve(10000);
            List <long>   numbers = new List <long>();
            List <string> results = new List <string>();

            int limit = 10000;

            foreach (long n in primes)
            {
                if (n > 1000)
                {
                    numbers.Add(n);
                }
            }

            for (int i = 0; i < numbers.Count(); i++)
            {
                for (int j = i + 1; j < numbers.Count; j++)
                {
                    long k = numbers[j] + (numbers[j] - numbers[i]);

                    if (k < limit && numbers.Contains(k))
                    {
                        if (Formulas.IsPermutation((int)numbers[i], (int)numbers[j]) && Formulas.IsPermutation((int)numbers[i], (int)k))
                        {
                            if (numbers[i] != 1487)
                            {
                                results.Add(String.Concat(numbers[i], numbers[j], k));
                            }
                        }
                    }
                }
            }
            return(Int64.Parse(results[0]));
        }
Ejemplo n.º 4
0
        public static long Problem50()
        {
            long result   = 0L;
            int  resCount = 0;
            long limit    = 1000000;

            List <long> primes = Formulas.PrimeSieve(limit);

            for (int i = primes.Count - 1; i >= 0; i--)
            {
                /*
                 * The prime 41, can be written as the sum of six consecutive primes:
                 *
                 * 41 = 2 + 3 + 5 + 7 + 11 + 13
                 * This is the longest sum of consecutive primes that adds to a prime below one-hundred.
                 *
                 * The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
                 *
                 * Which prime, below one-million, can be written as the sum of the most consecutive primes?
                 */

                long l = 0L;
                long j = i;
                int  c = 0;

                while (l < limit && j >= 0)
                {
                    l += primes.ElementAt((int)j);
                    j--;
                    c++;

                    if (c > resCount && Formulas.isPrime(l))
                    {
                        resCount = c;
                        result   = l;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        public static long Problem41()
        {
            /*
             * We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
             * What is the largest n-digit pandigital prime that exists?
             */

            List <long> primes = Formulas.PrimeSieve(10000000);

            long result = 0L;

            foreach (long p in primes)
            {
                if (Formulas.isPandigital(p))
                {
                    result = p;
                }
            }

            return(result);
        }