/*
* 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.
*/
public class Problem4 {
int maxPalindrome() {
int max = 0;
int product;
for (int i = 100; i < 1000; i++) {
for(int j = 100; j < 1000; j++) {
product = i * j;
if(isPalindrome(product) && product >max) {
max = product;
}
}
}
return max;
}
boolean isPalindrome(Integer x) {
String str = x.toString();
char[] digitArray = str.toCharArray();
int length = digitArray.length;
for(int i = 0; i < length/2; i++ ) {
if (digitArray[i] != digitArray[length-i-1]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Problem4 problem = new Problem4();
// 906609
System.out.println(problem.maxPalindrome());
}
}
isPalindrome -> 스트링으로 받아서 str.equal(reverse(str)) 을 리턴했으면 더 우아했을 것