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?
My easiest guess is to multiply all prime numbers till the number.
def get_primes_smaller(number):
primes = []
prospects = [n for n in range(2, number)]
while prospects:
p = prospects[0]
prospects = [x for x in prospects if x % p != 0]
primes.append(p)
return primes
from operator import mul
from functools import reduce
def get_number_which_is_divisible_by_all_numbers_from_one_to(n):
ps= get_primes_smaller(n + 1)
return reduce(mul, ps, 1)
print(get_number_which_is_divisible_by_all_numbers_from_one_to(10))
That obviously didn't work. The reason is that the same prime can occur multiple times in the factorization of a divisor. For example $2^{3} = 8$. We can always brute force of course. We do a smart brute force and only check multiples from the product of primes because this factor must be part of the solution.
def is_divisible_by_numbers_smaller_or_equal(number, maximum_number):
for n in range(2, maximum_number + 1):
if number % n != 0:
return False
return True
def get_number_which_is_divisible_by_all_numbers_from_one_to(n):
ps = get_primes_smaller(n + 1)
factor = reduce(mul, ps, 1)
multiples_of_factor = factor
while True:
if is_divisible_by_numbers_smaller_or_equal(multiples_of_factor, n):
return multiples_of_factor
multiples_of_factor += factor
assert(get_number_which_is_divisible_by_all_numbers_from_one_to(10) == 2520)
print(get_number_which_is_divisible_by_all_numbers_from_one_to(20))