139 lines
2.9 KiB
Python
Executable File
139 lines
2.9 KiB
Python
Executable File
#!/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?
|
|
#
|
|
|
|
import decorators # Typically imported to compute execution duration of functions.
|
|
import time # Typically imported for sleep function, to slow down execution in terminal.
|
|
import typing
|
|
import pprint
|
|
|
|
# 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(factor_list)
|
|
|
|
for i in factor_list:
|
|
common.append(find_prime_factors(i))
|
|
|
|
pprint.pprint(common)
|
|
|
|
for j in common:
|
|
for k in j:
|
|
product *= k
|
|
|
|
print(product)
|
|
|
|
|
|
|
|
# print("The largest palindrome number which is a product of two numbers of length {} is {} ... ".format("foo_list","foo"))
|
|
|
|
main() |