Dingle Berries
Dingle Berries

Reputation: 11

A palindromic number reads the same both ways. Find the largest palindrome made from the product of two 3-digit numbers

This my solution to ProjectEuler's problem 4 "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.

When I run this program it does not display anything in the console and I am not sure why because I don't see a problem with my code.

public class Problem4 
{
    static boolean end = false;
    public static void main(String[] args)
    {
        int multiplier = 999;
        int product = 0;
        while(end = false )
        {
            product = multiplier * 999;
            isPalindrome(product);
            multiplier--;
        }
    }
    public static Boolean isPalindrome(int number)
    {   
        int b = (Integer.valueOf(number).toString().length() - 1);
        char[] storage = new    char[Integer.valueOf(number).toString().length()];
        char[] reverse = new char[Integer.valueOf(number).toString().length()];
        for(int i = 0; i < Integer.valueOf(number).toString().length(); i++)
        {
            storage[i] = (Integer.valueOf(number).toString().charAt(i));
        }
        for(int a = 0; a < Integer.valueOf(number).toString().length(); a++)
        {
            reverse[a] = storage[b];
            b--;
        }
        String compare = "";
        for(int x = 0; x < Integer.valueOf(number).toString().length(); x++)
        {
            compare += reverse[x];
        }
        if (compare.equals(Integer.valueOf(number).toString()))
        {
            System.out.println(number);
            end = true;
            return true;
        }
        return false;
    }
}

Upvotes: 1

Views: 393

Answers (1)

mgaido
mgaido

Reputation: 3055

Your problem is easy:

while (end = false)

should be in your intentions:

while (end == false) or even better while (!end)

Otherwise it means that you are assigning false to end, and then evaluating end, which is always false.

For reference, in future I'd recommend you to try using the debugger for such cases.

Upvotes: 1

Related Questions