Solved problem 40.

This commit is contained in:
2018-05-19 19:42:26 -04:00
parent 2ef44eda3c
commit 9ad9b1de27
3 changed files with 12023 additions and 0 deletions

View File

@@ -0,0 +1,118 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Champernowne's constant (Euler Problem 40)\n",
"\n",
"<https://projecteuler.net/problem=40>\n",
"\n",
"An irrational decimal fraction is created by concatenating the positive integers:\n",
"\n",
" 0.123456789101112131415161718192021...\n",
"\n",
"It can be seen that the 12th digit of the fractional part is 1.\n",
"\n",
"If dn represents the nth digit of the fractional part, find the value of the following expression.\n",
"\n",
"$d_{1} × d_{10} × d_{100} × d_{1000} × d_{10000} × d_{100000} × d_{1000000}$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I do not see why we cannot hold $8 bytes \\times 10^{6} = 8 MB$ in memory."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def get_irrational_fraction(digits_max):\n",
" s = []\n",
" n = 1\n",
" digits = 0\n",
" while digits < digits_max:\n",
" s.append(str(n))\n",
" digits += len(str(n))\n",
" n += 1\n",
" return \"\".join(s)\n",
"\n",
"assert(get_irrational_fraction(20)[12] == \"1\")\n",
"\n",
"f = get_irrational_fraction(1000001)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, we got the fractional. No we get the digits at the different positions and calculate the product. For that purpose we reuse our poduct function from problem 8."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def product(xs):\n",
" from operator import mul\n",
" from functools import reduce\n",
" return reduce(mul, xs, 1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"210\n"
]
}
],
"source": [
"s = product(map(int, [f[10**i - 1] for i in range(7)]))\n",
"print(s)\n",
"assert(s == 210)"
]
}
],
"metadata": {
"completion_date": "Sun, 20 May 2018, 00:40",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.5",
"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": [
"champernowne",
"product",
"fraction"
]
},
"nbformat": 4,
"nbformat_minor": 2
}