Lychrel Number Implementation

5

Lychrel Number is a natural number that cannot form a palindrome through the iterative process of repeatedly reversing its digits and adding the resulting numbers. The process is sometimes called the 196-algorithm, after the most famous number associated with the process.
The first few numbers not known to produce palindromes when applying the 196-algorithm (i.e., a reverse-then-add sequence) are sometimes known as Lychrel numbers.
Examples:

Input : 56
Output : 56 is lychrel  : false
Explanation : 56 becomes palindromic after one iteration : 
56 + 65 = 121

Input : 196
Output : 196 is lychrel  : true
Explanation : 196 becomes palindromic after 19 iterations :
196 + 691 = 887
887 + 788 = 1675
1675 + 5761 = 7436
7436 + 6347 = 13783
13783 + 38731 = 52514
....
16403234045 + 54043230461
70446464506 + 60546464407

The task is to find if a given number is Lycheral with given limit on number of iterations.

1. Iterate given number of times
    1. Add number to it's reverse
    2. If 
         the newly formed number is palindrome
       then
          return false  // Number is not lychrel.
2. return true         // Number is lychrel
// Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
import java.io.*;

public class LychrelNumberTest
{
    // Max Iterations
    private static int MAX_ITERATIONS = 20;

    // Function to check whether number is Lychrel Number
    private static boolean isLychrel(long number)
    {
        for (int i = 0; i < MAX_ITERATIONS; i++)
        {
            number = number + reverse(number);
            if (isPalindrome(number))
                return false;

        }
        return true;
    }

    // Function to check whether the number is Palindrome
    private static boolean isPalindrome(final long number)
    {
        return number == reverse(number);
    }

    // Function to reverse the number
    private static long reverse(long number)
    {
        long reverse = 0;

        while (number > 0)
        {
            long remainder = number % 10;
            reverse = (reverse * 10) + remainder;
            number = number / 10;
        }
        return reverse;
    }

    // driver program
    public static void main(String[] args)
    {
        long number = 295;
        System.out.println(number + " is lychrel ? "
                           + isLychrel(number));
    }
}

Output:

196 is lychrel ? true

This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



5 Average Difficulty : 5/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.