Automatic commit performed through alias...

This commit is contained in:
Shaun Setlock
2020-05-30 19:10:39 -04:00
parent 925cb57d70
commit 46960540a2
6 changed files with 865 additions and 6 deletions

View File

@@ -102,7 +102,6 @@ def evenly_divisible(candidate: int,factors: list):
@decorators.function_timer @decorators.function_timer
def main(): def main():
# Receive problem inputs... # Receive problem inputs...
smallest_factor = 1 smallest_factor = 1
largest_factor = 5 largest_factor = 5
@@ -114,7 +113,6 @@ def main():
maximum_solution_bound *= f maximum_solution_bound *= f
common = [] common = []
product = 1 product = 1
# #
# Brute force method below breaks down # Brute force method below breaks down
# and doesn't scale well ... # and doesn't scale well ...

View File

@@ -4,16 +4,27 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### Problem 5:\n", "# Problem 5:\n",
"\n", "\n",
"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.\n", "[Euler Project #5](https://projecteuler.net/problem=5)\n",
"\n", "\n",
"What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?\n" "> *2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.*\n",
"> *What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?*\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reserved Space For Imports\n",
"---"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -25,6 +36,134 @@
"import numpy" "import numpy"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reserved Space For Method Definition\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def primes_gen(start_n: int,max_n: int):\n",
" \"\"\"\n",
" Returns a generator object, containing the \n",
" primes inside a specified range.\n",
" primes_gen(start_n,max_n)\n",
" param 'start_n': Previous prime.\n",
" param 'max_n': Maximum integer allowed to be returned. Quit if reached.\n",
" \"\"\"\n",
" start_n += 1\n",
" for candidate in range(start_n,max_n):\n",
" notPrime = False\n",
" \n",
" if candidate in [0,1,2,3]:\n",
" yield candidate\n",
" for dividend in range(2,candidate):\n",
" \n",
" if candidate%dividend == 0:\n",
" notPrime = True\n",
" \n",
" if not notPrime:\n",
" yield candidate"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def evenly_divisible(candidate: int,factors: list):\n",
" \"\"\"\n",
" Determines if the supplied integer candidate is \n",
" evenly divisble by the supplied list of factors.\n",
" \n",
" evenly_divisible(candidate: int, factors: list)\n",
"\n",
" param 'candidate': Integer to be tested.\n",
" param 'factors': List of factors for the modulus operator.\n",
" \n",
" \"\"\"\n",
" \n",
" modulus_sum = 0\n",
"\n",
" for n in factors:\n",
" modulus_sum += candidate%n\n",
" \n",
" if modulus_sum == 0: \n",
" return True\n",
" else:\n",
" return False"
]
},
{
"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": [
"## 1. Begin with testing the problem statement's example case.\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Mathematically, the trick is to recognize that this a LCM (Least Common Multiple) problem. The solution is list all the prime factors of each number in the factor list, then compute their collective product.*\n",
"\n",
" - [x] Create the list of factors, prescribed by the problem statement. Use a list.\n",
" - [ ] Generate a list of prime factors for each of the factors. Use a list of list.\n",
" - [ ] For each of the unique prime factors found in the previous list of lists, \n",
" find the factor for which the \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Receive problem inputs...\n",
"smallest_factor = 1\n",
"largest_factor = 10\n",
"\n",
"# Compute intermediate inputs\n",
"factor_list = [int(i) for i in range(smallest_factor,largest_factor+1)]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,

46
problems/006_problem.py Executable file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env python
# Problem 6:
#
# The sum of the squares of the first ten natural numbers is,
# 1^2+2^2+...+10^2=385
#
# The square of the sum of the first ten natural numbers is,
# (1+2+...+10)^2=55^2=3025
#
# Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025385=2640
#
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
#
#
# Standard Imports:
# -------------------
import time # Typically imported for sleep function, to slow down execution in terminal.
import typing
import sys
import pprint
# Imports from Virtual Environment for this Project:
# ---------------------------------------------------
sys.path.append('/home/shaun/code/github/euler_project/py_euler_project/venv')
import numpy
# Imports from Modules Built for this Project:
# -----------------------------------------------
sys.path.append('/home/shaun/code/github/euler_project/py_euler_project/problems')
import decorators
@decorators.function_timer
def main():
pass
main()

View File

@@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 6:\n",
"\n",
"[Euler Project #6](https://projecteuler.net/problem=6)\n",
"\n",
"> *The sum of the squares of the first ten natural numbers is,\n",
"1^2+2^2+...+10^2=385\n",
"\n",
">The square of the sum of the first ten natural numbers is,\n",
"(1+2+...+10)^2=55^2=3025\n",
"\n",
">Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025385=2640.\n",
"\n",
">Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.*\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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reserved Space For Method Definition\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def sum_of_squares(i: int,j: int):\n",
" '''\n",
" Function that computes the sum of a series of squared integers.\n",
" '''\n",
" series = [k**2 for k in range(i,j+1)]\n",
" #pprint.pprint(series)\n",
" summ = sum(series)\n",
" return summ"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def square_of_sum(m: int,n: int):\n",
" '''\n",
" Function that computes the square of a summation of a series of integers.\n",
" '''\n",
" series = [p for p in range(m,n+1)]\n",
" summ = sum(series)**2\n",
" #print(summ)\n",
" return summ"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Begin with testing the problem statement's example case.\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Mathematically, the trick is to recognize that this a LCM (Least Common Multiple) problem. The solution is list all the prime factors of each number in the factor list, then compute their collective product.*\n",
"\n",
" - [ ] Create a method that performs the first computation.\n",
" - [ ] Create a method that performs the second computation.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25164150\n"
]
}
],
"source": [
"# Receive problem inputs...\n",
"smallest_factor = 1\n",
"largest_factor = 100\n",
"\n",
"a = square_of_sum(smallest_factor,largest_factor) - sum_of_squares(smallest_factor,largest_factor)\n",
"print(a)\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": []
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View 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
}

View File

@@ -0,0 +1,169 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 9:\n",
"\n",
"[Euler Project #9](https://projecteuler.net/problem=9)\n",
"\n",
"\n",
"\n",
">A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,\n",
"a2 + b2 = c2\n",
"\n",
">For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.\n",
"\n",
">There exists exactly one Pythagorean triplet for which a + b + c = 1000.\n",
"Find the product abc.\n",
"\n",
"\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": "markdown",
"metadata": {},
"source": [
"#### 1. b) Build out the candidate array. "
]
},
{
"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": 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
}