From 9ad9b1de27af45b18b570995ef6973dcb7876ff5 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Sat, 19 May 2018 19:42:26 -0400 Subject: [PATCH] Solved problem 40. --- ipython/EulerProblem040.html | 11890 ++++++++++++++++++++++++++++++++ ipython/EulerProblem040.ipynb | 118 + ipython/index.html | 15 + 3 files changed, 12023 insertions(+) create mode 100644 ipython/EulerProblem040.html create mode 100644 ipython/EulerProblem040.ipynb diff --git a/ipython/EulerProblem040.html b/ipython/EulerProblem040.html new file mode 100644 index 0000000..f0e1ae5 --- /dev/null +++ b/ipython/EulerProblem040.html @@ -0,0 +1,11890 @@ + + + +EulerProblem040 + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+

Champernowne's constant (Euler Problem 40)

https://projecteuler.net/problem=40

+

An irrational decimal fraction is created by concatenating the positive integers:

+ +
0.123456789101112131415161718192021...
+
+
+

It can be seen that the 12th digit of the fractional part is 1.

+

If dn represents the nth digit of the fractional part, find the value of the following expression.

+

$d_{1} × d_{10} × d_{100} × d_{1000} × d_{10000} × d_{100000} × d_{1000000}$

+ +
+
+
+
+
+
+
+

I do not see why we cannot hold $8 bytes \times 10^{6} = 8 MB$ in memory.

+ +
+
+
+
+
+
In [1]:
+
+
+
def get_irrational_fraction(digits_max):
+    s = []
+    n = 1
+    digits = 0
+    while digits < digits_max:
+        s.append(str(n))
+        digits += len(str(n))
+        n += 1
+    return "".join(s)
+
+assert(get_irrational_fraction(20)[12] == "1")
+
+f = get_irrational_fraction(1000001)
+
+ +
+
+
+ +
+
+
+
+
+

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.

+ +
+
+
+
+
+
In [2]:
+
+
+
def product(xs):
+    from operator import mul
+    from functools import reduce
+    return reduce(mul, xs, 1)
+
+ +
+
+
+ +
+
+
+
In [3]:
+
+
+
s = product(map(int, [f[10**i - 1] for i in range(7)]))
+print(s)
+assert(s == 210)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
210
+
+
+
+ +
+
+ +
+
+
+ + + + + + diff --git a/ipython/EulerProblem040.ipynb b/ipython/EulerProblem040.ipynb new file mode 100644 index 0000000..af16d78 --- /dev/null +++ b/ipython/EulerProblem040.ipynb @@ -0,0 +1,118 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Champernowne's constant (Euler Problem 40)\n", + "\n", + "\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 +} diff --git a/ipython/index.html b/ipython/index.html index adf98ef..3163a17 100644 --- a/ipython/index.html +++ b/ipython/index.html @@ -589,6 +589,21 @@ + + Problem 040 + Sun, 20 May 2018, 00:40 + Problem 040 + + + champernowne + + product + + fraction + + + + Problem 067 Fri, 5 Sep 2014, 07:36