Luís Ramalho bio photo

Luís Ramalho

Developer & Entrepreneur

Twitter LinkedIn Github Stackoverflow

Lychrel numbers is a problem in which one must find how many Lychrel numbers are there below ten-thousand. You can read here more about Lychrel numbers.

It was clear since the beginning that we would be dealing with large numbers, so my approach was to use BigIntegers.

int lychrelNumbers = 0;
int i = 0;
BigInteger temp;
BigInteger one = BigInteger.ONE;
BigInteger b = new BigInteger("10");
BigInteger limit = new BigInteger("10000");
for (; b.compareTo(limit) < 0; b = b.add(one), i = 0) {
    temp = b;
    do {
        temp = temp.add(reverse(temp));
        i++;
    } while (!isPalindrome(temp.toString()) && i < 50);

    // Lychrel number
    if (i == 50) {
        lychrelNumbers++;
    }
}
return lychrelNumbers;

I started with 10 because 0 to 9 are palindromes. Inside the for loop we check if the addition of a number with it’s reverse is a palindrome for a maximum of 49 times. If not, then we got a Lychrel numbers.

The full solution can be found here.