Automatic commit performed through alias...
This commit is contained in:
65
problems/008_problem/008_problem.py
Executable file
65
problems/008_problem/008_problem.py
Executable file
@@ -0,0 +1,65 @@
|
||||
# # Problem 8:
|
||||
#
|
||||
# [Euler Project #8](https://projecteuler.net/problem=8)
|
||||
#
|
||||
#
|
||||
#
|
||||
# > The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
|
||||
#
|
||||
# > 73167176531330624919225119674426574742355349194934
|
||||
# 96983520312774506326239578318016984801869478851843
|
||||
# 85861560789112949495459501737958331952853208805511
|
||||
# 12540698747158523863050715693290963295227443043557
|
||||
# 66896648950445244523161731856403098711121722383113
|
||||
# 62229893423380308135336276614282806444486645238749
|
||||
# 30358907296290491560440772390713810515859307960866
|
||||
# 70172427121883998797908792274921901699720888093776
|
||||
# 65727333001053367881220235421809751254540594752243
|
||||
# 52584907711670556013604839586446706324415722155397
|
||||
# 53697817977846174064955149290862569321978468622482
|
||||
# 83972241375657056057490261407972968652414535100474
|
||||
# 82166370484403199890008895243450658541227588666881
|
||||
# 16427171479924442928230863465674813919123162824586
|
||||
# 17866458359124566529476545682848912883142607690042
|
||||
# 24219022671055626321111109370544217506941658960408
|
||||
# 07198403850962455444362981230987879927244284909188
|
||||
# 84580156166097919133875499200524063689912560717606
|
||||
# 05886116467109405077541002256983155200055935729725
|
||||
# 71636269561882670428252483600823257530420752963450
|
||||
#
|
||||
# > Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||
#
|
||||
# ---
|
||||
import os
|
||||
import pprint
|
||||
import time # Typically imported for sleep function, to slow down execution in terminal.
|
||||
import typing
|
||||
import decorators # Typically imported to compute execution duration of functions.
|
||||
import numpy
|
||||
import pandas
|
||||
|
||||
# problem statement input 1000 digit integer
|
||||
input_list = [int(n) for n in "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"]
|
||||
|
||||
# problem statement request series length
|
||||
series_len = 13
|
||||
|
||||
rows = len(input_list) - series_len
|
||||
|
||||
columns = series_len + 1
|
||||
|
||||
array = numpy.array(numpy.ones((rows,columns)))
|
||||
|
||||
# loop for each candidate
|
||||
for i in range(rows):
|
||||
# loop to fill out each candidate and store its product in the last column
|
||||
for j in range(series_len):
|
||||
|
||||
array[i][j] = input_list[i+j]
|
||||
array[i][-1] *= array[i][j]
|
||||
|
||||
|
||||
# Cheat by using pandas to print out the maximum product and its associated series of integers.
|
||||
df = pandas.DataFrame(array)
|
||||
|
||||
print(df.sort_values(by=series_len,ascending=False).head(1))
|
||||
331
problems/008_problem/008_problem_jnotebook.ipynb
Normal file
331
problems/008_problem/008_problem_jnotebook.ipynb
Normal file
@@ -0,0 +1,331 @@
|
||||
{
|
||||
"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",
|
||||
"<br/>\n",
|
||||
"<br/>\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": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>0</th>\n",
|
||||
" <th>1</th>\n",
|
||||
" <th>2</th>\n",
|
||||
" <th>3</th>\n",
|
||||
" <th>4</th>\n",
|
||||
" <th>5</th>\n",
|
||||
" <th>6</th>\n",
|
||||
" <th>7</th>\n",
|
||||
" <th>8</th>\n",
|
||||
" <th>9</th>\n",
|
||||
" <th>10</th>\n",
|
||||
" <th>11</th>\n",
|
||||
" <th>12</th>\n",
|
||||
" <th>13</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>197</th>\n",
|
||||
" <td>5.0</td>\n",
|
||||
" <td>5.0</td>\n",
|
||||
" <td>7.0</td>\n",
|
||||
" <td>6.0</td>\n",
|
||||
" <td>6.0</td>\n",
|
||||
" <td>8.0</td>\n",
|
||||
" <td>9.0</td>\n",
|
||||
" <td>6.0</td>\n",
|
||||
" <td>6.0</td>\n",
|
||||
" <td>4.0</td>\n",
|
||||
" <td>8.0</td>\n",
|
||||
" <td>9.0</td>\n",
|
||||
" <td>5.0</td>\n",
|
||||
" <td>2.351462e+10</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"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
|
||||
}
|
||||
Reference in New Issue
Block a user