Where would you like to go, Toady? RSS 2.0
# Monday, February 09, 2009

There is certainly a more efficient algorithm solve Euler #4, but here is my solution.  Runtime averages around 500ms.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Diagnostics;

namespace Euler4
{
    class Program
    {
        static void Main(string[] args)
        {
            long maxPal = 0;
            long iMax = 0;
            long jMax = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 100; i <= 999; i++)
            {
                for (int j = 100; j <= 999; j++)
                {
                    long curPal = (long)(i * j);
                    if (CheckPallindrome(curPal))
                    {
                        if (curPal > maxPal)
                        {
                            maxPal = curPal;
                            iMax = i;
                            jMax = j;
                        }
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("Runtime was " + sw.ElapsedMilliseconds + " ms");
            Console.WriteLine("Runtime was " + sw.ElapsedTicks + " ticks ");
            Console.WriteLine(iMax + " x " + jMax + " = " + maxPal);
            Console.ReadLine();
            
        }

        private static bool CheckPallindrome(long input)
        {
            PallindromeChecker pc = new PallindromeChecker();
            pc.Pal = input;
            return pc.IsPallindrome();
        }
    }

    class PallindromeChecker
    {
        private long _pal;
        private string forward, reverse;

        public long Pal
        {
            get
            {
                return _pal;
            }
            set
            {
                _pal = value;
            }
        }


        public bool IsPallindrome()
        {
            GetReversedLong();
            if (forward == reverse)
                return true;

            return false;
        }

        private void GetReversedLong()
        {
            forward = Convert.ToString(_pal);
            StringBuilder temp2 = new StringBuilder();

            char[] arr = new char[forward.Length - 1];
            arr = forward.ToCharArray();
            for (int i = forward.Length - 1; i >= 0; i--)
            {
                temp2.Append(arr[i]);
            }
            reverse = temp2.ToString();
        }
    }
}
Monday, February 09, 2009 10:26:20 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Bookmark, Tweet, or Share
Stackoverflow.com Rep
Flickr stream
Archive
<February 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Chris Ballance
Sign In
Statistics
Total Posts: 46
This Year: 11
This Month: 2
This Week: 0
Comments: 11
All Content © 2010, Chris Ballance

Valid XHTML 1.0 Transitional