"This could get tricky if we were working with fixed size integers, since the reverse-and-add process for [Lychrel numbers](https://en.wikipedia.org/wiki/Lychrel_number) can result in some big numbers. But Python ints have unlimited precision, so no need to worry about that."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5588d5d2",
"metadata": {},
"outputs": [],
"source": [
"def is_palindrome(n):\n",
" s = str(n)\n",
" return s == s[::-1]\n",
"\n",
"\n",
"def is_lychrel_number(n):\n",
" for _ in range(0, 50):\n",
" rev = int(str(n)[::-1])\n",
" n += rev\n",
" if is_palindrome(n):\n",
" return False\n",
" \n",
" return True"
]
},
{
"cell_type": "markdown",
"id": "d45f3460",
"metadata": {},
"source": [
"This is easy to brute force, since we're not checking that many numbers, and we loop at most 50 times in our test."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e473996a",
"metadata": {},
"outputs": [],
"source": [
"lychrel_numbers = {n for n in range(1, 10000) if is_lychrel_number(n)}"
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."