143 lines
3.0 KiB
Plaintext
143 lines
3.0 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Euler Problem 16\n",
|
||
|
"\n",
|
||
|
"$2^{15}$ = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.\n",
|
||
|
"\n",
|
||
|
"What is the sum of the digits of the number $2^{1000}$?"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Seriously? Okay, probably more difficult in C."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"1366\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"s = sum(map(int, str(2**1000)))\n",
|
||
|
"assert(s == 1366)\n",
|
||
|
"print(s)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Okay, let's do it without big ints."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 18,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def double_number_string(number_string):\n",
|
||
|
" number_string = number_string[::-1] # reverse\n",
|
||
|
" result = []\n",
|
||
|
" carriage = 0\n",
|
||
|
" for c in number_string:\n",
|
||
|
" s = carriage + int(c) + int(c)\n",
|
||
|
" carriage = s // 10\n",
|
||
|
" next_digit = s % 10\n",
|
||
|
" result.append(str(next_digit))\n",
|
||
|
" if carriage > 0:\n",
|
||
|
" result.append(str(carriage))\n",
|
||
|
" result = \"\".join(result)[::-1]\n",
|
||
|
" return result\n",
|
||
|
"\n",
|
||
|
"assert(double_number_string(\"132\") == \"264\")\n",
|
||
|
"assert(double_number_string(\"965\") == \"1930\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 19,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"1366\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def powers_of_two(n):\n",
|
||
|
" \"\"\" Retuns nth power of two as a string. \"\"\"\n",
|
||
|
" assert(n >= 0)\n",
|
||
|
" if n == 0:\n",
|
||
|
" return \"1\"\n",
|
||
|
" s = \"2\"\n",
|
||
|
" for _ in range(n - 1):\n",
|
||
|
" s = double_number_string(s)\n",
|
||
|
" return s\n",
|
||
|
"\n",
|
||
|
"assert(powers_of_two(3) == \"8\")\n",
|
||
|
"assert(powers_of_two(10) == \"1024\")\n",
|
||
|
"\n",
|
||
|
"number_string = powers_of_two(1000)\n",
|
||
|
"print(sum(map(int, number_string)))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Here we go. The conversion to integer and the sum would be easy in C."
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"completion_date": "Sun, 31 Aug 2014, 20:49",
|
||
|
"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.4"
|
||
|
},
|
||
|
"tags": [
|
||
|
"c",
|
||
|
"manual"
|
||
|
]
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0
|
||
|
}
|