Write a program to find if a number is Lychrel Number in Python

Upasana | May 05, 2019 | 1 min read | 99 views | Python Coding Problems


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. for example,

  • 56 becomes palindromic after one iteration: 56+65 = 121.

  • 57 becomes palindromic after two iterations: 57+75 = 132, 132+231 = 363.

Lychrel Number: Python implementation
def reverse(num):
    rev = 0
    while(num > 0):
        rem = num %10
        rev = (rev *10) + rem
        num = num //10
    return rev

def check_palindrome(num):
    temp = num
    rev = 0

    while temp != 0:
        rev = (rev * 10) + (temp % 10)
        temp = temp // 10

    if num == rev:
        return False
    else:
        return True

def lychrel_num(num):
    i = 0
    type = str(num) + " is a lychrel number"
    while check_palindrome(num):
        num = num + reverse(num)
        i =i+1
        if i == 10:
            type = str(num) + " is not a lychrel number"
            break
    return i, num, type

print(lychrel_num(196))
print(lychrel_num(87))
Output
(10, 18211171, '196 is not a lychrel number')
(4, 4884, '87 is a lychrel number')

Top articles in this category:
  1. Find if credit card number is valid or not
  2. Write a program to check if the given word is Isogram & Pair isogram in python
  3. Python coding challenges for interviews
  4. Write a program for Hailstone Sequence in Python
  5. Write a python program to find Largest Substring that occurs more than once
  6. Derivative of 1/x & Tossing a coin problem
  7. Find extra long factorials in python

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.