diff --git a/problems/005_problem.py b/problems/005_problem.py index 61ef414..00200c5 100755 --- a/problems/005_problem.py +++ b/problems/005_problem.py @@ -102,7 +102,6 @@ def evenly_divisible(candidate: int,factors: list): @decorators.function_timer def main(): - # Receive problem inputs... smallest_factor = 1 largest_factor = 5 @@ -114,7 +113,6 @@ def main(): maximum_solution_bound *= f common = [] product = 1 - # # Brute force method below breaks down # and doesn't scale well ... diff --git a/problems/005_problem_jnotebook.ipynb b/problems/005_problem_jnotebook.ipynb index 15dc51e..175d856 100644 --- a/problems/005_problem_jnotebook.ipynb +++ b/problems/005_problem_jnotebook.ipynb @@ -4,16 +4,27 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "#### Problem 5:\n", + "# Problem 5:\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", - "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", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -25,6 +36,134 @@ "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", "execution_count": null, diff --git a/problems/006_problem.py b/problems/006_problem.py new file mode 100755 index 0000000..b6a8df1 --- /dev/null +++ b/problems/006_problem.py @@ -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 3025−385=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() + diff --git a/problems/006_problem_jnotebook.ipynb b/problems/006_problem_jnotebook.ipynb new file mode 100644 index 0000000..14c1120 --- /dev/null +++ b/problems/006_problem_jnotebook.ipynb @@ -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 3025−385=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 +} diff --git a/problems/008_problem_jnotebook.ipynb b/problems/008_problem_jnotebook.ipynb new file mode 100644 index 0000000..de5622e --- /dev/null +++ b/problems/008_problem_jnotebook.ipynb @@ -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", + "
\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 +} diff --git a/problems/009_problem_jnotebook.ipynb b/problems/009_problem_jnotebook.ipynb new file mode 100644 index 0000000..6155abd --- /dev/null +++ b/problems/009_problem_jnotebook.ipynb @@ -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", + "
\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": "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 +}