Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
n=10^9 in 2 minutes solution in Speedy category for Ugly Numbers by StefanPochmann
from collections import deque
from itertools import repeat
def ugly_number(n: int) -> int:
U = deque()
V = deque()
popU, pushU = U.popleft, U.append
popV, pushV = V.popleft, V.append
u, v, w, vw = 2, 3, 5, 3
for _ in repeat(None, n - 1):
if u <= vw:
pushU(u)
u = popU() << 1
elif v <= w:
pushU(v)
pushV(v)
v = popV()
v += v << 1
vw = v if v <= w else w
else:
pushU(w)
pushV(w)
w += w << 2
vw = v
return U[-1] if U else n
Dec. 12, 2021
Comments: