All pastes #467820 Raw Edit

Mike: problem4

public java v1 · immutable
·published 2007-05-02 18:59 UTC
rendered paste body
package net.nereson.euler.problem04;import net.nereson.euler.StopWatch;/** * <h1>Problem 04</h1> * * <p>A palindromic number reads the same both ways. The largest palindrome made from * the product of two 2-digit numbers is 9009 = 91 × 99.</p> * * <p>Find the largest palindrome made from the product of two 3-digit numbers.</p> * * @author <a href="mailto:@gmail.com">mnereson</a> * @see <a href="http://projecteuler.net/index.php?section=view&id=20">Euler; Problem 20</a> */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());    }}