Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q36\n", "Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2." ] }, { "cell_type": "code", "execution_count": 1, "id": "255771bb-8696-4c1c-aeb2-f56c3bc62237", "metadata": {}, "outputs": [], "source": [ "def palindromic_creator(n):\n", " str_n = str(n)\n", " return [int(str_n+str_n[::-1]),int(str_n[:-1]+str_n[::-1])]\n", "\n", "def dec_to_bin(dec):\n", " return bin(dec).replace(\"0b\", \"\")\n", "\n", "def is_palindromic_base2(dec):\n", " str_base2 = dec_to_bin(dec)\n", " return str_base2 == str_base2[::-1]\n", "\n", "def palindromic_base2_and_base10(n):\n", " half_range = int(str(n)[0]) * 10 ** ((len(str(n))+1)//2)\n", " result = set()\n", " for i in range(1,half_range):\n", " for palindromic_base10 in palindromic_creator(i):\n", " if is_palindromic_base2(palindromic_base10):\n", " result.add(palindromic_base10)\n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "id": "63599c30-aa22-42ed-9680-d7cd9c0f1f39", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 9.51 ms, sys: 0 ns, total: 9.51 ms\n", "Wall time: 8.77 ms\n" ] }, { "data": { "text/plain": [ "39347399" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum(palindromic_base2_and_base10(1000000))" ] }, { "cell_type": "code", "execution_count": 3, "id": "e2e0b12b-2840-488c-896a-b823ce0ea05c", "metadata": {}, "outputs": [], "source": [ "def palindromic_base2_and_base10_2(n):\n", " base10_half_range = int(str(n)[0]) * 10 ** ((len(str(n))+1)//2)\n", " palindromic_base10 = { i for n in range(base10_half_range) for i in palindromic_creator(n) }\n", " palindromic_base2 = { int(str(i),base=2) for n in range(base10_half_range) for i in palindromic_creator(dec_to_bin(n)) }\n", " return palindromic_base10.intersection(palindromic_base2)" ] }, { "cell_type": "code", "execution_count": 4, "id": "14194b11-c66f-49f3-8cc5-9e54ede02f95", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 27.2 ms, sys: 243 µs, total: 27.5 ms\n", "Wall time: 25.4 ms\n" ] }, { "data": { "text/plain": [ "39347399" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum(palindromic_base2_and_base10_2(1000000))" ] }, { "cell_type": "code", "execution_count": null, "id": "6d652702-c6eb-4125-8236-97e39a2f1ca0", "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 Q35
Next Notebook:
Project Euler Q37
Loading