Automatic commit performed through alias...

This commit is contained in:
Shaun Setlock
2020-08-02 07:38:10 -04:00
parent 675a507520
commit b7bfa36d06
11 changed files with 67 additions and 0 deletions

View File

@@ -0,0 +1,150 @@
#!/usr/bin/env python
# Problem 5:
#
# 2520 is the smallest number that can
# be divided by each of the numbers
# from 1 to 10 without any remainder.
#
# What is the smallest positive number
# that is evenly divisible by all of the
# numbers from 1 to 20?
#
# 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
# Create function that finds the next
# prime number when supplied with an
# intitial integer.
def primes_gen(start_n: int,max_n: int):
"""
Returns a generator object, containing the
primes inside a specified range.
primes_gen(start_n,max_n)
param 'start_n': Previous prime.
param 'max_n': Maximum
"""
start_n += 1
for candidate in range(start_n,max_n):
notPrime = False
if candidate in [0,1,2,3]:
yield candidate
for dividend in range(2,candidate):
if candidate%dividend == 0:
notPrime = True
if not notPrime:
yield candidate
def find_prime_factors(n: int):
"""
Return a list object containing all
prime factors of the provided integer, n.
find_prime_factors(n)
param 'n': The integer to be analyzed.
"""
returned_prime = list(primes_gen(0,n))
pprint.pprint(returned_prime)
return returned_prime
def evenly_divisible(candidate: int,factors: list):
"""
Determines if the supplied integer candidate is
evenly divisble by the supplied list of factors.
evenly_divisible(candidate: int, factors: list)
param 'candidate': Integer to be tested.
param 'factors': List of factors for the modulus operator.
"""
modulus_sum = 0
for n in factors:
modulus_sum += candidate%n
if modulus_sum == 0:
return True
else:
return False
@decorators.function_timer
def main():
# Receive problem inputs...
smallest_factor = 1
largest_factor = 5
# Compute intermediate inputs
factor_list = [int(i) for i in range(smallest_factor,largest_factor+1)]
maximum_solution_bound = 1
for f in factor_list:
maximum_solution_bound *= f
common = []
product = 1
#
# Brute force method below breaks down
# and doesn't scale well ...
#
# # Initialize loop parameters
# n = 1
# test_passed = False
#
# while n<maximum_solution_bound and not test_passed:
# test_passed = evenly_divisible(n,factor_list)
# if not test_passed:
# n += 1
# 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.
pprint.pprint(common)
for j in common:
for k in j:
product *= k
print(product)
print(numpy.array(common))
# print("The largest palindrome number which is a product of two numbers of length {} is {} ... ".format("foo_list","foo"))
main()

View File

@@ -0,0 +1,203 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Problem 5:\n",
"\n",
"[Euler Project #5](https://projecteuler.net/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",
"> *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": 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 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,
"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
}