{ "cells": [ { "cell_type": "markdown", "id": "950160e4", "metadata": {}, "source": [ "# [Double-base Palindromes](https://projecteuler.net/problem=36)\n", "\n", "Python's `bin` function converts an integer to a string representing the number in binary, starting with `0b`, so we'll just slice that off before we reverse and check if it's a [palindrome](https://en.wikipedia.org/wiki/Palindromic_number)." ] }, { "cell_type": "code", "execution_count": 1, "id": "39bf1e12", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1,\n", " 3,\n", " 5,\n", " 7,\n", " 9,\n", " 33,\n", " 99,\n", " 313,\n", " 585,\n", " 717,\n", " 7447,\n", " 9009,\n", " 15351,\n", " 32223,\n", " 39993,\n", " 53235,\n", " 53835,\n", " 73737,\n", " 585585}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "is_base_2_palindrome = lambda x: bin(x)[2:] == bin(x)[:1:-1]\n", "is_base_10_palindrome = lambda x: str(x) == str(x)[::-1]\n", "\n", "double_palindromes = {n for n in range(1, 1000000) if is_base_2_palindrome(n) and is_base_10_palindrome(n)}\n", "double_palindromes" ] }, { "cell_type": "code", "execution_count": 2, "id": "ce61e551", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "872187" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(double_palindromes)" ] }, { "cell_type": "markdown", "id": "b83cbc0d", "metadata": {}, "source": [ "## Relevant sequences\n", "* Numbers that are base-2 and base-10 palindromes: [A007632](https://oeis.org/A007632)\n", "\n", "#### Copyright (C) 2025 filifa\n", "\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)." ] } ], "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 }