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.

Firstly I decided to try and solve the problem using two 2-digit numbers so I can understand how the creator of this problem got to 9009.

In about 15 minutes I was able to come up with this brute force hack:

public class Problem4 { public static void main(String[] args) { solve(); } private static void solve() { int highestPalindrome = 0; for (int left = 99; left > 2; left--) { for (int right = 99; right > 2; right--) { int candidate = left * right; if (isPalindrome(candidate)) { System.out.println(format("Palindrome found! Using %d * %d = %d ", left, right, candidate)); if (candidate > highestPalindrome) { highestPalindrome = candidate; } } } } System.out.println("Highest palindrome is " + highestPalindrome); } private static boolean isPalindrome(int palindrome) { String palindromeString = "" + palindrome; String reversed = new StringBuilder(palindromeString).reverse().toString(); return palindromeString.equals(reversed); } }

Which gives the output of

Highest palindrome is 9009

Excellent, so we’re on the right track. The next thing I did was alter the loop statements to start at 999, which then prints the following

Highest palindrome is 906609

Which is the correct answer!

Any suggestions on improvements? Please comment!