From ee38e14979eeb7357fc818e1e5ed15195a9cf17f Mon Sep 17 00:00:00 2001 From: filifa Date: Sun, 25 May 2025 23:12:08 -0400 Subject: [PATCH] add problem 55 --- notebooks/problem0055.ipynb | 105 ++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 notebooks/problem0055.ipynb diff --git a/notebooks/problem0055.ipynb b/notebooks/problem0055.ipynb new file mode 100644 index 0000000..550d114 --- /dev/null +++ b/notebooks/problem0055.ipynb @@ -0,0 +1,105 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "de629cc8", + "metadata": {}, + "source": [ + "# [Lychrel Numbers](https://projecteuler.net/problem=55)\n", + "\n", + "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)}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2c3f1cbf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "249" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(lychrel_numbers)" + ] + }, + { + "cell_type": "markdown", + "id": "ef8a3b03", + "metadata": {}, + "source": [ + "## Relevant sequences\n", + "* Suspected Lychrel numbers: [A023108](https://oeis.org/A023108)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "SageMath 9.5", + "language": "sage", + "name": "sagemath" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}