Palindroms

Palindroms are strings or numbers that start and end with the same character sequence, for example ABCBA or 12321.

Is Number a Palindrom?

public class IsNumberPalindrom {

    public static void main(String... args) {
        IsNumberPalindrom is = new IsNumberPalindrom();

        System.out.println(is.isPalindrome(1));
        System.out.println(is.isPalindrome(-1));
        System.out.println(is.isPalindrome(1221));
    }

    public boolean isPalindrome(int x) {
        String text = String.valueOf(x);
        int start = 0;
        int end = text.length() - 1;
        while(start < end) {
            if (text.charAt(start) != text.charAt(end)) {
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}

Lychrel Number

Every number that does not become palindrom after couple of interations is called Lychrel number. Here is an example.

If we send there number like 1960000, it finds 3 palindroms in 1000 interactions. Interesting is that if we use 196 (Lychrel number), we are not able to find Palindrom (nobody found one yet).

When we run this for 1960000, the algorithm finds the following palindroms.

Last updated

Was this helpful?