add problem 36
This commit is contained in:
parent
c7d559ca9c
commit
9e57ca0200
|
@ -0,0 +1,69 @@
|
|||
{
|
||||
"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": [
|
||||
"872187"
|
||||
]
|
||||
},
|
||||
"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",
|
||||
"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)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
Loading…
Reference in New Issue