VOOZH about

URL: https://www.geeksforgeeks.org/dsa/lychrel-number-implementation/

⇱ Lychrel Number Implementation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lychrel Number Implementation

Last Updated : 23 Jul, 2025

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 Lychrel with a given limit on the 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

Following are the steps to solve this problem :

  1. Create functions to check for palindromes and reverse numbers.
  2. Define a function that iterates a certain amount of times while looking for Lychrel numbers.
  3. Initialize a long integer variable with the number to be checked in the main function.
  4. Call the isLychrel function and store the result.
  5. Print the input number and the isLychrel function's result.
  6. Run the program while changing the input numbers. 

Below is the implementation of the above approach :


Output: 
295 is lychrel ? true

 

Complexity Analysis :

  • Time Complexity  : O(log N) ; it is because time complexity of the reverse and isPalindrome functions is O(log N), where N is the number of digits in the input number 
  • Auxiliary Space : O(1) ; this is constant since it does not depend on the input size


 

Comment
Article Tags: