0

So I'm trying to find the largest product of 2 3-digit numbers that is a palindrome. Here's my code:

class project_euler4 {
    public static int largest_palindrome = 0;

    public static boolean isPalindrome(int number) {
        String original = Integer.toString(number);
        String reversed = new StringBuffer(original).reverse().toString();
        return (original == reversed) ? true : false;
    }

    public static void main(String[] args) {
        for (int i = 100; i < 1000; i++) {
            for (int j = 100; j < 1000; j++) {
                int candidate = i * j;
                if (isPalindrome(candidate) && candidate > largest_palindrome) {
                    largest_palindrome = candidate;
                }
            }
        }
        System.out.println("The largest palindrome made from the product of 2 3-digit numbers is " + largest_palindrome);
    }
}

When I compile and run, I get:

The largest palindrome made from the product of 2 3-digit numbers is 0

So for some reason, my largest_palindrome variable isn't getting updated when a product is a palindrome. I suspect it has something to do with my isPalindrome() function, but am not sure.

Any ideas?

Thanks for the help, Mariogs

anon_swe
  • 8,791
  • 24
  • 85
  • 145
  • 3
    Here's a performance optimization tip; start at 999 and work backward. The first palindrome is then your largest palindrome. – Elliott Frisch May 29 '14 at 21:05

1 Answers1

1

You used == instead of .equals() to compare the strings. Classic mistake.

Epiglottal Axolotl
  • 1,048
  • 8
  • 17