Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q40\n", "If \\(d_n\\) represents the n-th digit of the fractional part, find the value of the following expression.\n", "\n", "\\[d_{1} \\times d_{10} \\times d_{100} \\times d_{1000} \\times d_{10000} \\times d_{100000} \\times d_{1000000} \\]" ] }, { "cell_type": "code", "execution_count": 1, "id": "255771bb-8696-4c1c-aeb2-f56c3bc62237", "metadata": {}, "outputs": [], "source": [ "def prod(x_vector):\n", " result = 1\n", " for i in x_vector:\n", " result *= i\n", " return result\n", "\n", "def champernownes_constant(n_vector):\n", " max_n = max(n_vector)\n", " max_range = int(max_n*10/9+10) // (len(str(max_n))-1) if max_n > 9 else max_n\n", " champernownes_str = \"\".join(str(i) for i in range(1,max_range))\n", " return prod(int(champernownes_str[n-1]) for n in n_vector)" ] }, { "cell_type": "code", "execution_count": 2, "id": "3d047ff9-52dc-4b20-b614-4192212ea5ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 26.1 ms, sys: 0 ns, total: 26.1 ms\n", "Wall time: 24.2 ms\n" ] }, { "data": { "text/plain": [ "210" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "champernownes_constant([1,10,100,1000,10000,100000,1000000])" ] }, { "cell_type": "code", "execution_count": 3, "id": "eaffcd54-a14c-4ba7-b8c6-0d2ed6ecffc6", "metadata": {}, "outputs": [], "source": [ "def n_champernownes(n):\n", " num_digit = 0\n", " n_len = 0\n", " temp_sum = 0\n", " while n > n_len:\n", " temp_sum += n_len\n", " num_digit += 1\n", " n_len = num_digit*9*10**(num_digit-1)\n", " n -= 1\n", " return int(str((n-temp_sum)//num_digit+10**(num_digit-1))[(n-temp_sum)%num_digit])\n", "\n", "def champernownes_constant_2(n_vector):\n", " return prod(n_champernownes(n) for n in n_vector)" ] }, { "cell_type": "code", "execution_count": 4, "id": "8a58a1fe-aba0-4cfb-b966-918249cd00a7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 14 µs, sys: 6 µs, total: 20 µs\n", "Wall time: 21.7 µs\n" ] }, { "data": { "text/plain": [ "210" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "champernownes_constant_2([1,10,100,1000,10000,100000,1000000])" ] }, { "cell_type": "code", "execution_count": null, "id": "820c6f59-d5d1-4f67-a379-b41d8bb337ac", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q39
Next Notebook:
Project Euler Q41
Loading