-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths005-smallest-multiple.py
52 lines (43 loc) · 1.38 KB
/
s005-smallest-multiple.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Project Euler 5 - Smallest Multiple
# GitHub: urtuba
from functools import reduce
from collections import defaultdict
def find_factors(num:int) -> defaultdict:
'''
Finds factors of a number and their recurrence to create the number.
:param num: the number
:return: default dict, keys represent factors, values are recurrence
'''
factors = dict()
factors = defaultdict(lambda: 0, factors)
start = 2
while True:
if num % start == 0:
factors[start] += 1
num = num / start
else:
start += 1
if start > num:
break
return factors
def smallest_evenly_divisible(num:int) -> int:
'''
Takes a number and finds the smallest number which is evenly divisible
to all numbers upto given number.
:param num: the number
:return: smallest evenly divisible number
'''
all_factors = defaultdict(lambda: 0, dict())
for num in range(2, num):
num_factors = find_factors(num)
for key, val in num_factors.items():
if all_factors[key] < val:
all_factors[key] = val
factors_list = []
for key, val in all_factors.items():
for i in range(val):
factors_list.append(key)
return reduce(lambda x, y: x*y, factors_list)
if __name__ == '__main__':
result = smallest_evenly_divisible(20)
print(result)