RosettaCodeData/Task/Hamming-numbers/Python/hamming-numbers-3.py

17 lines
403 B
Python
Raw Permalink Normal View History

2018-06-22 20:57:24 +00:00
from heapq import heappush, heappop
from itertools import islice
2013-04-10 21:29:02 -07:00
2018-06-22 20:57:24 +00:00
def h():
heap = [1]
while True:
h = heappop(heap)
while heap and h==heap[0]:
heappop(heap)
for m in [2,3,5]:
heappush(heap, m*h)
yield h
2013-04-10 21:29:02 -07:00
2018-06-22 20:57:24 +00:00
print list(islice(h(), 20))
print list(islice(h(), 1690, 1691))
print list(islice(h(), 999999, 1000000)) # runtime 9.5 sec on i5-3570S