[Project Euler] Problem 52

Problem Description

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.

 

C#

This problem is simple, we just try each number one by one.

        // this is just a guess.
        private const int MIN_VALUE = 10000;

        public static void Run()
        {
            int num = MIN_VALUE;

            while (true)
            {
                var charArray = num.ToString().ToCharArray();
                // if the higest digit of this num is not 1, then we add num by one digit
                if (charArray[0] != '1')
                {
                    num = (int)Math.Pow(10, charArray.Length) + 1;
                }

                // this is an anonymous function to get the sorted digit string of num
                Func<int, string> GetDigitStr = delegate(int n)
                {
                    return string.Concat(n.ToString().ToCharArray().OrderBy(c => c));
                };

                var digitStr = GetDigitStr(num);

                bool isOK = true;
                for (int i = 2; i <= 6; i++)
                {
                    int temp = num * i;

                    var tempStr = GetDigitStr(temp);
                    if (tempStr != digitStr)
                    {
                        isOK = false;
                        break;
                    }
                }

                if (isOK)
                {
                    Console.WriteLine("result = {0}", num);
                    break;
                }
                num++;
            }
 
        }
原文地址:https://www.cnblogs.com/quark/p/2557402.html