Tuesday, March 3, 2015

Project Euler - Largest palindrome product

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.
https://projecteuler.net/problem=4

    public class Euler4_Palindrome
    {
        int product;
        string productString;
        string reversedProductString;
        List palindromeList = new List();

        public int Solve() 
        {
            for (int i = 999; i > 99; i--)
            {
                for (int j = 999; j > 99; j--)
                {
                    product = i * j;

                    productString = product.ToString();
                    reversedProductString = Reverse(productString);
                    
                    if (productString == reversedProductString) palindromeList.Add(product); 
                }
            }
            
            palindromeList.Sort();
            return palindromeList.Last();
        }

        public static string Reverse(string s)
        {
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }
    }