113 lines
2.3 KiB
Plaintext
113 lines
2.3 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Euler Problem 36\n",
|
||
|
"\n",
|
||
|
"The decimal number, 585 = $1001001001_2$ (binary), is palindromic in both bases.\n",
|
||
|
"\n",
|
||
|
"Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.\n",
|
||
|
"\n",
|
||
|
"(Please note that the palindromic number, in either base, may not include leading zeros.)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"This looks like an easy problem to me. Like really easy. Should we try to brute force first?"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def is_palindrome_decimal(n):\n",
|
||
|
" return str(n) == str(n)[::-1]\n",
|
||
|
"\n",
|
||
|
"assert(is_palindrome_decimal(232) == True)\n",
|
||
|
"assert(is_palindrome_decimal(2) == True)\n",
|
||
|
"assert(is_palindrome_decimal(2322) == False)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def is_palindrome_binary(n):\n",
|
||
|
" s = str(bin(n)[2:])\n",
|
||
|
" return s == s[::-1]\n",
|
||
|
"\n",
|
||
|
"assert(is_palindrome_binary(585) == True)\n",
|
||
|
"assert(is_palindrome_binary(3) == True)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"collapsed": true
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"s = sum([i for i in range(1000000) if is_palindrome_decimal(i) and is_palindrome_binary(i)])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"872187\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(s)"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"completion_date": "Fri, 11 May 2018, 02:38",
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"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.5.5"
|
||
|
},
|
||
|
"tags": [
|
||
|
"palindrome",
|
||
|
"brute force"
|
||
|
]
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0
|
||
|
}
|