Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Ugly Numbers by pokosasa
from heapq import heappop, heappush
MAX_N = 1500
ugly_numbers = []
used = {1}
que = [1]
while que and len(ugly_numbers) < MAX_N:
a = heappop(que)
ugly_numbers.append(a)
for p in [2, 3, 5]:
if a * p not in used:
heappush(que, a * p)
used.add(a * p)
def ugly_number(n: int) -> int:
return ugly_numbers[n - 1]
if __name__ == "__main__":
print("Example:")
print(ugly_number(4))
# These "asserts" are used for self-checking and not for an auto-testing
assert ugly_number(4) == 4
assert ugly_number(6) == 6
assert ugly_number(11) == 15
print("Ugly Numbers coding complete? Click 'Check' to earn cool rewards!")
Dec. 11, 2021
Comments: