1
\$\begingroup\$

This is a challenge I found here. You're suppose to list how many steps it takes to convert a number to a palindromic number, where each step consists of adding number to the reverse of itself. For example, 196196871 takes 151 steps and gives me 3588816724276188853.

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

    private Scanner in = new Scanner(System.in);

    public Main() {
        String input = in.nextLine();
        ArrayList< Long > targets = new ArrayList< Long >();

        // Checks if the ENTER key was pressed. ENTER is the exit key of this program.
        while(!input.equals(""))
        {
            targets.add(Long.parseLong(input));
            input = in.nextLine();
        }

        for(long target : targets)
            System.out.println(target + " gets palindromic after " + getPalSteps(target) + " steps: " + convertToPal(target));          
    }

    public long reverse(long number) {
        long reverse = 0;
        long n = number;
        while (n != 0)
        {
            reverse *= 10;
            reverse = reverse + n % 10;
            n /= 10;
        }
        return reverse;
    }

    public boolean palindrome(long number) {
        return reverse(number) == number;
    }

    public long convertToPal(long number) {
        if (number == 196)
            return -1;

        while (!palindrome(number))
        {
            number = reverse(number) + number;
        }

        return number;
    }

    public long getPalSteps(long number) {
        int steps = 0;

        if (number == 196)
            return -1;

        while (!palindrome(number))
        {
            number = reverse(number) + number;
            steps++;
        }

        if(steps > 10000)
            System.out.println("Lychrel number!");

        return steps;
    }

    public static void main(String[] args) {
        new Main();
    }

}
\$\endgroup\$
2
  • \$\begingroup\$ 3588816724276188853 looks like a palindromic number to me, in what way is it not working perfectly? \$\endgroup\$ Commented Jun 14, 2015 at 14:59
  • \$\begingroup\$ The challenge output says "196196871 gets palindromic after 45 steps: 4478555400006996000045558744". My output is different, it takes more steps and gives me a different number. \$\endgroup\$ Commented Jun 14, 2015 at 16:43

2 Answers 2

4
\$\begingroup\$

When you add a number to its reverse, its value approximately doubles, on average. If you do it 10000 times, you should get a result that is very roughly 210000 times the original. That will certainly overflow a long, which only holds values below 263. You need to either

  • lower the iteration limit
  • check for overflow
  • use BigInteger
  • reinvent arithmetic for large numbers using strings or arrays

The use of OOP is awkward and inappropriate. Constructing an object should just involve just allocating and initializing some memory to model something. Your Main() constructor, though, is the entire input, calculation, and output loop of the program.

Your program isn't object-oriented at all. Just make everything static, and merge the constructor code into the main() function.

\$\endgroup\$
3
\$\begingroup\$

Overall your code looks very good. It's very readable and easy to understand.

Don't Do Work Twice

My only real suggestion is to not do the work twice. You're first checking how many steps it takes to convert the number to a palindromic number, and then doing it again to actually convert it. It should all happen in one step, like this:

Pair<long> convertToPal(long number)
{
    int steps = 0;

    if (number == 196)
    {
        return new Pair<-1, 196>;
    }

    while (!palindrome(number))
    {
        number = reverse(number) + number;
        steps++;
    }

    if(steps > 10000)
        System.out.println("Lychrel number!");

    return new Pair<steps, number>;
}

Note: there may be better ways to return 2 values than a Pair. You could create a class that holds the resulting number and the number of steps, or whatever you feel is appropriate. (See here for more on that.)

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.