Thursday, January 16, 2014

Project Euler Solution using C#: Problem 4: Largest Palindrome Product

Problem:

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.
My Solution: 


  1.         static void Main(string[] args)
  2.         {
  3.             int min = 100;
  4.             int max = 999;
  5.             List<int> list = new List<int>();
  6.             int multiplied=0;>
  7.             for (int i = min; i < max; i++)
  8.             {
  9.                 for (int j = min; j < max; j++)
  10.                 {
  11.                     multiplied = i * j;
  12.                     string reversed = new string(multiplied.ToString().ToCharArray().Reverse().ToArray());
  13.                     if (reversed == multiplied.ToString())
  14.                     {
  15.                         list.Add(multiplied);
  16.                     }
  17.                 }
  18.             }
  19.             Console.WriteLine("the largest palindrome made from the product of two 3-digit numbers is : " + list.Max());
  20.             Console.ReadLine();
  21.         }
Note: You can simplifies the coding :)

No comments:

Post a Comment