-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem5.py
38 lines (30 loc) · 947 Bytes
/
problem5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Smallest multiple
# 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 logging
import operator
from functools import reduce
from collections import Counter
logging.basicConfig(level = logging.DEBUG)
def factor(x):
f = Counter()
f[1] += 1
i = 2
while(x != 1):
if(x%i==0):
x = x/i
f[i] += 1
else:
i += 1
return f
all_factors = [factor(n) for n in range(1, 21)]
smallest_factors = {}
for factors in all_factors:
for k,v in factors.items():
if k not in smallest_factors or (k in smallest_factors and smallest_factors[k]<v):
smallest_factors[k] = v
smallest_multiple = reduce(operator.mul, [k**v for k,v in smallest_factors.items()])
logging.debug(smallest_multiple)