Problem 4: 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 x 99. Find the largest palindrome made from the product of two 3-digit numbers.
The Catch
How to check if a number is palindromic.
The Light
Parse a number to a character array. There are 2 possibilities:
The length of the array is even: from the 2 middle-indexed elements, check outward for equality.
The length of the array is odd: from the elements to the right and left of the middle-indexed element, check outward for equality.
The Code
public class Problem4
{
public static void main(String[] args)
{
int max = 0;
int value = 0;
for(int i = 100; i < 1000; i++)
{
for(int j = 100; j < 1000; j++)
{
value = i * j;
String s = Integer.toString(value);
if(isPalin(s))
if(value > max)
max = value;
}
}
System.out.println(max);
}
public static boolean isPalin(String s)
{
for(int i = 0; i < s.length()/2; i++)
{
if(s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
}