June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,25 +1,16 @@
|
|||
from itertools import tee, chain, groupby, islice
|
||||
from heapq import merge
|
||||
from heapq import heappush, heappop
|
||||
from itertools import islice
|
||||
|
||||
def raymonds_hamming():
|
||||
# Generate "5-smooth" numbers, also called "Hamming numbers"
|
||||
# or "Regular numbers". See: http://en.wikipedia.org/wiki/Regular_number
|
||||
# Finds solutions to 2**i * 3**j * 5**k for some integers i, j, and k.
|
||||
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
|
||||
|
||||
def deferred_output():
|
||||
for i in output:
|
||||
yield i
|
||||
|
||||
result, p2, p3, p5 = tee(deferred_output(), 4)
|
||||
m2 = (2*x for x in p2) # multiples of 2
|
||||
m3 = (3*x for x in p3) # multiples of 3
|
||||
m5 = (5*x for x in p5) # multiples of 5
|
||||
merged = merge(m2, m3, m5)
|
||||
combined = chain([1], merged) # prepend a starting point
|
||||
output = (k for k,g in groupby(combined)) # eliminate duplicates
|
||||
|
||||
return result
|
||||
|
||||
print list(islice(raymonds_hamming(), 20))
|
||||
print islice(raymonds_hamming(), 1689, 1690).next()
|
||||
print islice(raymonds_hamming(), 999999, 1000000).next()
|
||||
print list(islice(h(), 20))
|
||||
print list(islice(h(), 1690, 1691))
|
||||
print list(islice(h(), 999999, 1000000)) # runtime 9.5 sec on i5-3570S
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue