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

Carvia Tech | May 05, 2019 at 08:34 AM | 3 views | Python Coding Problem


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. Top 100 interview questions on Data Science & Machine Learning
  2. Write a program for Hailstone Sequence in Python
  3. Write a program to check if the given word is Isogram & Pair isogram in python
  4. Write a python program to find Largest Substring that occurs more than once
  5. python problem 1: find the runner-up score
  6. Count the number of open lockers in school
  7. Find perfect abundant or deficient factors in python


Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 month ago


Recommended books for interview preparation:
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel

This website uses cookies to ensure you get the best experience on our website. more info