Moved 1 to 5 to Python and learned a ton.

This commit is contained in:
2019-07-13 23:58:38 -04:00
parent 414cf4b074
commit b246d56acd
13 changed files with 402 additions and 2 deletions

29
python/e005.py Normal file
View File

@@ -0,0 +1,29 @@
from lib_prime import prime_factors_count
from lib_misc import product
def get_number_divisible(n):
""" Returns the lowest number that is divisible
by all numbers from 1 to n. We calculate the factors
for all numbers and make sure that the minimum number
for each factor is included in the solution. """
f = {}
for i in range(2, n + 1):
for prime, count in prime_factors_count(i).items():
try:
f[prime] = max(f[prime], count)
except KeyError:
f[prime] = count
n = product([prime**count for prime, count in f.items()])
return n
assert(get_number_divisible(10) == 2520)
def euler_005():
return get_number_divisible(20)
assert(euler_005() == 232792560)
print("e005.py: {}".format(euler_005()))