Exemplo n.º 1
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);
        }
Exemplo n.º 2
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]));
        }
Exemplo n.º 3
0
        public static long Problem24()
        {
            /*
             * A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
             *
             * 012   021   102   120   201   210
             *
             * What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
             */
            List <string> permutations = new List <string>();

            string seed = "0123456789";

            Formulas.Permute(seed.ToArray(), 0, 9, permutations);
            permutations.Sort();

            Console.Out.WriteLine("Found " + permutations.Count() + " permutations");

            return(Int64.Parse(permutations[999999]));
        }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo n.º 6
0
        public static long Problem36()
        {
            /*
             * The decimal number, 585 = 1001001001 (binary), is palindromic in both bases.
             * Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
             * (Please note that the palindromic number, in either base, may not include leading zeros.)
             */

            long result = 0L;

            for (int i = 0; i < 1000000; i++)
            {
                string b = Convert.ToString(i, 2);
                if (Formulas.IsPalindrome(b) && Formulas.IsPalindrome(Convert.ToString(i)))
                {
                    Console.Out.WriteLine(i + " -> " + b);
                    result += i;
                }
            }

            return(result);
        }
Exemplo n.º 7
0
        public static long Problem206()
        {
            //Smarter bruteforce (387 420 489) possibilities
            Stopwatch sw = new Stopwatch();

            sw.Start();

            decimal start = Math.Floor(Formulas.Sqrt(10203040506070809.0m) / 10.0m);
            decimal end   = Math.Floor(Formulas.Sqrt(19293949596979899.0m) / 10.0m);
            long    dr    = -1L;

            RegexUtil.setExpression("1.2.3.4.5.6.7.8.9");

            for (long i = (long)start; i < (long)end; i++)
            {
                dr = i * 10 + 3;
                if (RegexUtil.isMatching((dr * dr).ToString()))
                {
                    break;
                }

                dr = i * 10 + 7;
                if (RegexUtil.isMatching((dr * dr).ToString()))
                {
                    break;
                }

                if (i % 10000 == 0)
                {
                    Console.WriteLine(((i - start) / (end - start) * 100.0m) + "%");
                }
            }
            sw.Stop();

            return((long)(dr * 10.0m));
        }
Exemplo n.º 8
0
        public static long Problem52()
        {
            /*
             * It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.
             * Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits
             */
            int  x  = 0;
            bool ok = true;

            do
            {
                x++;
                ok = true;
                for (int i = 2; i < 7; i++)
                {
                    if (!Formulas.IsPermutation(x * i, x))
                    {
                        ok = false;
                    }
                }
            } while (!ok);

            return(x);
        }
Exemplo n.º 9
0
 public static long Problem38()
 {
     return(Formulas.LargestPandigital(1, 9));
 }