The Wayback Machine - https://web.archive.org/web/20240708015243/https://www.geeksforgeeks.org/lychrel-number-implementation/
Open In App

Lychrel Number Implementation

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 :

C++




// C++ Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
#include<iostream>
using namespace std;
 
long reverse(long);
bool isPalindrome(long);
 
// Max Iterations
static int MAX_ITERATIONS = 20;
 
// Function to check whether number is
// Lychrel Number
string 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
bool isPalindrome(long number)
{
    return number == reverse(number);
}
 
// Function to reverse the number
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
int main()
{
    long number = 295;
    cout<<number << " is lychrel? "
                       << isLychrel(number);
}
 
// This code is contributed by Smitha


Java




// Java 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));
    }
}


Python3




# Python3 Program to check whether the given number
# is Lychrel Number or not with given limit
# on number of iterations.
 
# Max Iterations
MAX_ITERATIONS = 20;
 
# Function to check whether number is
# Lychrel Number
def isLychrel(number):
     
    for i in range(MAX_ITERATIONS):
        number = number + reverse(number);
         
        if (isPalindrome(number)):
            return "false";
     
    return "true";
 
# Function to check whether the number
# is Palindrome
def isPalindrome(number):
  
    return number == reverse(number);
 
# Function to reverse the number
def reverse(number):
  
    reverse = 0;
    while (number > 0):
      
        remainder = number % 10;
        reverse = (reverse * 10) + remainder;
        number = int(number / 10);
     
    return reverse;
 
# Driver Code
number = 295;
print(number," is lychrel? ",isLychrel(number));
 
# This code is contributed by mits


C#




// C# Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
using System;
 
class GFG
{
    // Max Iterations
    private static int MAX_ITERATIONS = 20;
 
    // Function to check whether number is Lychrel Number
    private static bool 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 bool isPalindrome( 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()
    {
        long number = 295;
        Console.Write(number + " is lychrel? "
                        + isLychrel(number));
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP Program to check whether the given number
// is Lychrel Number or not with given limit
// on number of iterations.
 
// Max Iterations
$MAX_ITERATIONS = 20;
 
// Function to check whether number is
// Lychrel Number
function isLychrel($number)
{
    global $MAX_ITERATIONS;
    for ($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
function isPalindrome($number)
{
    return $number == reverse($number);
}
 
// Function to reverse the number
function reverse($number)
{
    $reverse = 0;
    while ($number > 0)
    {
        $remainder = $number % 10;
        $reverse = ($reverse * 10) + $remainder;
        $number = (int)($number / 10);
    }
     
    return $reverse;
}
 
// Driver Code
$number = 295;
echo $number . " is lychrel? " .
             isLychrel($number);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript Program to check
// whether the given number is Lychrel
// Number or not with given limit
// on number of iterations.
 
// Max Iterations
var MAX_ITERATIONS = 20;
 
// Function to check whether
// number is Lychrel Number
function isLychrel(number)
{
    for (var 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
function isPalindrome(number)
{
    return number == reverse(number);
}
 
// Function to reverse the number
function reverse(number)
{
    var reverse = 0;
 
    while (number > 0)
    {
        var remainder = number % 10;
        reverse = (reverse * 10) + remainder;
        number = parseInt(number / 10);
    }
    return reverse;
}
 
// driver program
var number = 295;
document.write(number + " is lychrel ? "
                + isLychrel(number));
 
 
// This code contributed by shikhasingrajput
 
</script>


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

 



Similar Reads

Implementation of Chinese Remainder theorem (Inverse Modulo based implementation)
We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that: x % num[0] = rem[0], x % num[1] = rem[1], ....................... x % num[k-1] = rem[k-1] Example: Input: num[] = {3, 4, 5}, rem[] = {2, 3, 1} Output: 11 Explanation: 11 is the sm
11 min read
Find the smallest positive number missing from an unsorted array : Hashing Implementation
Given an unsorted array with both positive and negative elements including 0. The task is to find the smallest positive number missing from the array in O(N) time. Examples: Input: arr[] = {-5, 2, 0, -1, -10, 15} Output: 1 Input: arr[] = {0, 1, 2, 3, 4, 5} Output: 6 Input: arr[] = {1, 1, 1, 0, -1, -2} Output: 2 We can use hashing to solve this prob
5 min read
Longest Monotonically Increasing Subsequence Size (N log N): Simple implementation
Given an array of random numbers, find the longest monotonically increasing subsequence (LIS) in the array. If you want to understand the O(NlogN) approach, it's explained very clearly here. In this post, a simple and time-saving implementation of O(NlogN) approach using stl is discussed. Below is the code for LIS O(NlogN): Implementation: C/C++ Co
5 min read
Binary Tree (Array implementation)
Given an array that represents a tree in such a way that array indexes are values in tree nodes and array values give the parent node of that particular index (or node). The value of the root node index would always be -1 as there is no parent for root. Construct the standard linked representation of given Binary Tree from this given representation
6 min read
Implementation of K Nearest Neighbors
Prerequisite: K nearest neighbors Introduction Say we are given a data set of items, each having numerically valued features (like Height, Weight, Age, etc). If the count of features is n, we can represent the items as points in an n-dimensional grid. Given a new item, we can calculate the distance from the item to every other item in the set. We p
10 min read
Morse Code Implementation
Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph. The algorithm is very simple. Every character in the English language is substituted by
9 min read
Linked List Implementation in C#
A LinkedList is a linear data structure which stores element in the non-contiguous location. The elements in a linked list are linked with each other using pointers. Or in other words, LinkedList consists of nodes where each node contains a data field and a reference(link) to the next node in the list. In C#, LinkedList is the generic type of colle
6 min read
Travelling Salesman Problem implementation using BackTracking
Travelling Salesman Problem (TSP): Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns back to the starting point.Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exist a tour that v
9 min read
Bellman Ford Algorithm (Simple Implementation)
We have introduced Bellman Ford and discussed on implementation here.Input: Graph and a source vertex src Output: Shortest distance to all vertices from src. If there is a negative weight cycle, then shortest distances are not calculated, negative weight cycle is reported.1) This step initializes distances from source to all vertices as infinite an
13 min read
Window to Viewport Transformation in Computer Graphics with Implementation
Window to Viewport Transformation is the process of transforming 2D world-coordinate objects to device coordinates. Objects inside the world or clipping window are mapped to the viewport which is the area on the screen where world coordinates are mapped to be displayed. General Terms: World coordinate - It is the Cartesian coordinate w.r.t which we
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg