-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_smallest_multiple.py
More file actions
executable file
·82 lines (55 loc) · 1.35 KB
/
Copy path5_smallest_multiple.py
File metadata and controls
executable file
·82 lines (55 loc) · 1.35 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3.7
from lib import timer
from functools import reduce
from math import sqrt, ceil
# can be improved but i'm bored with this problem, so bye
# def factorise(n):
# l=[]
# while n%2==0:
# n//=2
# l.append(2)
# for i in range(3, ceil(sqrt(n))+1, 2):
# while n%i==0:
# n/=i
# l.append(i)
# # return l if n<2 else n
# return l if n<2 else l+[n]
@timer
def main(N):
r = [2,3]
p = 6
for n in range(4,20):
for i in r:
if n%i==0: n//=i
r.append(n)
p*=n
return p
# ~20µ
################################################
# old approach - best ~21µ
# @timer
# def main(n):
# def _hcf(a, b): return a if b<1 else _hcf(b, a%b)
# def _lcm(a, b): return (a*b)//_hcf(a, b)
# return reduce(lambda a,b : _lcm(a, b), range(2, n))
# ~25µ
# same code as below (line 71 - 79), but runs faster
# apperently calling functions in python takes ~5µ
# update:
########################
# @timer
# def main(n):
# def _hcf(a, b): return a if b<1 else _hcf(b, a%b)
# return reduce(lambda a,b : (a*b)//_hcf(a, b), range(2, n))
# ~22µ i've no idea how !! I should just use c++
########################
#
# def lcm(*l):
# def _hcf(a, b): return a if b<1 else _hcf(b, a%b)
# def _lcm(a, b): return (a*b)//_hcf(a, b)
# return reduce(lambda a,b : _lcm(a, b), l)
# @timer
# def main(n):
# return lcm(*range(2, n))
# ~30µ
main(20)