Wednesday, September 10, 2014

Palindrome Product Challenge

Yet another coding challenge: 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.

  https://projecteuler.net/problem=4




int maxProduct = 0;

String product = "";
String productReverse = "";
for (int i = 1; i < 1000; i++) {
    for (int j = 1; j < 1000; j++) {
        product = Integer.toString(i * j);
        productReverse = (new StringBuffer(product)).reverse().toString();
        if (product.equals(productReverse)) {
            if (maxProduct < (i * j)) {
                maxProduct = i * j;
            }
        }
    }                        
}
System.out.println("result is: " + maxProduct);

No comments:

Post a Comment