Lychrel Numbers¶

This could get tricky if we were working with fixed size integers, since the reverse-and-add process for Lychrel numbers can result in some big numbers. But Python ints have unlimited precision, so no need to worry about that.

In [1]:
def is_palindrome(n):
    s = str(n)
    return s == s[::-1]


def is_lychrel_number(n):
    for _ in range(0, 50):
        rev = int(str(n)[::-1])
        n += rev
        if is_palindrome(n):
            return False
        
    return True

This is easy to brute force, since we're not checking that many numbers, and we loop at most 50 times in our test.

In [2]:
lychrel_numbers = {n for n in range(1, 10000) if is_lychrel_number(n)}
In [3]:
len(lychrel_numbers)
Out[3]:
249

Relevant sequences¶

  • Suspected Lychrel numbers: A023108

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.