-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
43 lines (38 loc) · 1.23 KB
/
Copy pathsolution.py
File metadata and controls
43 lines (38 loc) · 1.23 KB
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
import json
import time
import math
def prime_decomposition(n):
suspect_factor = 2
upper_bound = int(math.sqrt(n)) + 1
factors = {}
while suspect_factor < upper_bound:
if n % suspect_factor == 0:
if suspect_factor not in factors:
factors[suspect_factor] = 0
factors[suspect_factor] += 1
n = n // suspect_factor
else:
suspect_factor += 1
if n not in factors:
factors[n] = 0
factors[n] += 1
return factors
def smallest_multiple(n):
factors = {}
for i in range(1, n+1):
for factor, count in prime_decomposition(i).items():
if factor not in factors:
factors[factor] = count
elif factors[factor] < count:
factors[factor] = count
smallest_multiple = 1
for factor, count in factors.items():
smallest_multiple *= factor**count
return smallest_multiple
def solution() -> int:
return smallest_multiple(20)
cpu_s, wall_s = time.process_time(), time.time()
result = solution()
cpu_e, wall_e = time.process_time(), time.time()
cpu_time, wall_time = cpu_e - cpu_s, wall_e - wall_s
print(json.dumps({"solution": result, "cpu": cpu_time, "wall": wall_time}))