{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 8:\n", "\n", "[Euler Project #8](https://projecteuler.net/problem=8)\n", "\n", "\n", "\n", "> The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.\n", "\n", "> 73167176531330624919225119674426574742355349194934\n", "96983520312774506326239578318016984801869478851843\n", "85861560789112949495459501737958331952853208805511\n", "12540698747158523863050715693290963295227443043557\n", "66896648950445244523161731856403098711121722383113\n", "62229893423380308135336276614282806444486645238749\n", "30358907296290491560440772390713810515859307960866\n", "70172427121883998797908792274921901699720888093776\n", "65727333001053367881220235421809751254540594752243\n", "52584907711670556013604839586446706324415722155397\n", "53697817977846174064955149290862569321978468622482\n", "83972241375657056057490261407972968652414535100474\n", "82166370484403199890008895243450658541227588666881\n", "16427171479924442928230863465674813919123162824586\n", "17866458359124566529476545682848912883142607690042\n", "24219022671055626321111109370544217506941658960408\n", "07198403850962455444362981230987879927244284909188\n", "84580156166097919133875499200524063689912560717606\n", "05886116467109405077541002256983155200055935729725\n", "71636269561882670428252483600823257530420752963450\n", "\n", "> Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reserved Space For Imports\n", "---" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pprint\n", "import time # Typically imported for sleep function, to slow down execution in terminal.\n", "import typing\n", "import decorators # Typically imported to compute execution duration of functions.\n", "import numpy\n", "import pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reserved Space For Method Definition\n", "---" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Can we describe a few approaches to solving this problem?\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Let's discuss a few ways to work through this, then select one to implement.*\n", "\n", " 1. Create a 2D array in which we can place each candidate series of integers.\\\n", " A column vector can also be created in which can store the product of each series.\\\n", " If we zip these arrays together, sort on the product column, we can identify the\\\n", " the maximum product and its associate string of integers.\n", "
\n", "
\n", " 2. Create an array (of the specified length) to store a series of integers from the input\\\n", " number. Allow this to be an array that we use to compute the a product. It will shift as we\\\n", " slide along the input number. A second array of identical dimension, plus an additional place,\\\n", " could store the largest product, and the associated list of integers. \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Let's try the first approach!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. a) Take in problem statement information." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# problem statement input 1000 digit integer\n", "input_list = [int(n) for n in \"\\\n", "73167176531330624919225119674426574742355349194934\\\n", "96983520312774506326239578318016984801869478851843\\\n", "85861560789112949495459501737958331952853208805511\\\n", "12540698747158523863050715693290963295227443043557\\\n", "66896648950445244523161731856403098711121722383113\\\n", "62229893423380308135336276614282806444486645238749\\\n", "30358907296290491560440772390713810515859307960866\\\n", "70172427121883998797908792274921901699720888093776\\\n", "65727333001053367881220235421809751254540594752243\\\n", "52584907711670556013604839586446706324415722155397\\\n", "53697817977846174064955149290862569321978468622482\\\n", "83972241375657056057490261407972968652414535100474\\\n", "82166370484403199890008895243450658541227588666881\\\n", "16427171479924442928230863465674813919123162824586\\\n", "17866458359124566529476545682848912883142607690042\\\n", "24219022671055626321111109370544217506941658960408\\\n", "07198403850962455444362981230987879927244284909188\\\n", "84580156166097919133875499200524063689912560717606\\\n", "05886116467109405077541002256983155200055935729725\\\n", "71636269561882670428252483600823257530420752963450\"]\n", "\n", "# problem statement request series length\n", "series_len = 13" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. b) Build out the candidate array. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# number of possible integer-series candidates\n", "rows = len(input_list) - series_len\n", "# length of the requested series, plus an additional column to store the product\n", "columns = series_len + 1\n", "\n", "# construct the array with placeholder values\n", "array = numpy.array(numpy.ones((rows,columns)))\n", "\n", "# loop for each candidate\n", "for i in range(rows):\n", " # loop to fill out each candidate and store its product in the last column\n", " for j in range(series_len):\n", " \n", " array[i][j] = input_list[i+j]\n", " array[i][-1] *= array[i][j]\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. c) Cheat by using pandas to print out the maximum product and its associated series of integers. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df = pandas.DataFrame(array)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012345678910111213
1975.05.07.06.06.08.09.06.06.04.08.09.05.02.351462e+10
\n", "
" ], "text/plain": [ " 0 1 2 3 4 5 6 7 8 9 10 11 12 \\\n", "197 5.0 5.0 7.0 6.0 6.0 8.0 9.0 6.0 6.0 4.0 8.0 9.0 5.0 \n", "\n", " 13 \n", "197 2.351462e+10 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.sort_values(by=series_len,ascending=False).head(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }