-
Notifications
You must be signed in to change notification settings - Fork 0
/
twice_linear.py
48 lines (38 loc) · 986 Bytes
/
twice_linear.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
#!/usr/bin/env python
# https://www.codewars.com/kata/5672682212c8ecf83e000050
from time import time
def get_index(a, x) -> int:
lo = 0
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
midval = a[mid]
if midval < x:
lo = mid+1
elif midval > x:
hi = mid
else:
return -1
return mid if midval > x else mid+1
def dbl_linear(n):
u = [1]
i = 0
n_max = 3*n/2
for _ in range(int(n_max/2)):
new_y, new_z = 2*u[i] + 1, 3*u[i] + 1
new_y_i = get_index(u, new_y)
if new_y_i != -1:
u.insert(new_y_i, new_y)
new_z_i = get_index(u, new_z)
if new_z_i != -1:
u.insert(new_z_i, new_z)
i = i + 1
return u[n]
if __name__ == "__main__":
print(dbl_linear(10), 22)
print(dbl_linear(20), 57)
print(dbl_linear(30), 91)
print(dbl_linear(50), 175)
t_0 = time()
dbl_linear(50000)
print(time()-t_0)