Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q55\n", "How many Lychrel numbers are there below ten-thousand?" ] }, { "cell_type": "code", "execution_count": 1, "id": "bd7ddc2f-5964-49c3-a927-774399b0b0a9", "metadata": {}, "outputs": [], "source": [ "def is_palindrome(n):\n", " return str(n) == str(n)[::-1]\n", "\n", "def lychrel_numbers_under_n(n):\n", " result = set()\n", " for num in range(n,0,-1):\n", " temp = num\n", " for i in range(50):\n", " temp += int(str(temp)[::-1])\n", " if is_palindrome(temp):\n", " break\n", " if temp in result:\n", " result.add(num)\n", " break\n", " else:\n", " result.add(num)\n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "id": "3e78fa42-856e-4722-a279-245d9b8516a5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 19.1 ms, sys: 0 ns, total: 19.1 ms\n", "Wall time: 17.5 ms\n" ] }, { "data": { "text/plain": [ "249" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "len(lychrel_numbers_under_n(10000))" ] }, { "cell_type": "code", "execution_count": null, "id": "b869eac9-8c59-4a32-a226-a1308dec4e4c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q54
Next Notebook:
Project Euler Q56
Loading