Moved 1 to 5 to Python and learned a ton.
This commit is contained in:
29
python/e005.py
Normal file
29
python/e005.py
Normal 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()))
|
||||
Reference in New Issue
Block a user