【ProjectEuler】ProjectEuler_004

// A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91  99.
// 
// Find the largest palindrome made from the product of two 3-digit numbers.

using System;
using System.Collections.Generic;
using System.Text;

namespace projecteuler004
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime timeStart = DateTime.Now;
            int k;
            int maxPalindromin = int.MinValue;
            int maxI = 0;
            int maxJ = 0;
            for (int i = 999; i >= 100; i--)
            {

                int j;
                int dej;
                if (i % 11 == 0)
                {
                    j = 999;
                    dej = 1;
                }
                else
                {
                    j = 990;
                    dej = 11;
                }

                for (; j >= i; j-=dej)
                {
                    k = i * j;
                    if (isPalindromin(k) && maxPalindromin < k)
                    {
                        maxPalindromin = k;
                        maxI = i;
                        maxJ = j;
                    }
                }
            }
            Console.WriteLine("The largest palindrome made from the product of two 3-digit numbers is\n" + maxPalindromin + " = " + maxI + " * " + maxJ);
            Console.WriteLine("Total Milliseconds is " + DateTime.Now.Subtract(timeStart).TotalMilliseconds);
        }

        /// <summary>
        /// 判断是否为循环数
        /// </summary>  012345=6   01234=5
        /// <param name="number"></param>
        /// <returns></returns>
        private static bool isPalindromin(int number)
        {
            string numberStr = number.ToString();
            int lenth = numberStr.Length;
            for (int i = numberStr.Length / 2; i >= 0; i--)
            {
                if (numberStr[i] != numberStr[lenth - i - 1])
                {
                    return false;
                }
            }
            return true;
        }
    }
}

/*
The largest palindrome made from the product of two 3-digit numbers is
906609 = 913 * 993
Total Milliseconds is 32.0041

By GodMoon
*/

你可能感兴趣的:(【ProjectEuler】ProjectEuler_004)