Project Euler Problem 4
October 28th, 2009
1 comment
This one was easier than I expected. I considered using some sort of lookup table to limit unnecessary calculations (103 x 104 = 104 x 103), but I’m pretty sure that would have only created more overhead.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Project_Euler_4 { class Program { static void Main(string[] args) { new Program(); } Program() { int largestPalindromicProduct = 0; for (int i = 100; i <= 999; i++) for (int j = 100; j <= 999; j++) { int product = i * j; if (product > largestPalindromicProduct && isPalindromicNumber(product)) largestPalindromicProduct = product; } Console.WriteLine(largestPalindromicProduct); Console.ReadKey(); } bool isPalindromicNumber(int number) { string value = number.ToString(); for (int i = 0; i < value.Length; i++) { if (!value[i].Equals(value[value.Length - 1 - i])) return false; } return true; } } } |


