package net.nereson.euler.problem04; import net.nereson.euler.StopWatch; /** *

Problem 04

* *

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.

* * @author mnereson * @see Euler; Problem 20 */ public class Problem04 { public void run() { final int MAX_VALUE = 999; final int MIN_VALUE = 100; int a = MAX_VALUE; int b = MAX_VALUE; int test = 0; int keeper = 0; int keeperA = 0; int keeperB = 0; while (a >= MIN_VALUE) { while (b >= MIN_VALUE) { test = a * b; if (isPalindrome(test)) if (test > keeper) { keeper = test; keeperA = a; keeperB = b; } b--; } a--; b = MAX_VALUE; } System.out.print(keeperA + " * " + keeperB + " = "); System.out.println(keeper); } public boolean isPalindrome(int number) { String word = String.valueOf(number); int left = 0; int right = word.length() - 1; while (left < right) { if (word.charAt(left) != word.charAt(right)) return false; left++; right--; } return true; } public static void main(String[] args) { StopWatch stopWatch = new StopWatch("Problem04"); Problem04 problem04 = new Problem04(); problem04.run(); System.out.println(stopWatch.getTimeString()); } }