June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,38 +1,13 @@
|
|||
from itertools import islice, chain, tee
|
||||
from heapq import merge
|
||||
from itertools import tee
|
||||
|
||||
def merge(r, s):
|
||||
# This is faster than heapq.merge.
|
||||
rr = r.next()
|
||||
ss = s.next()
|
||||
while True:
|
||||
if rr < ss:
|
||||
yield rr
|
||||
rr = r.next()
|
||||
else:
|
||||
yield ss
|
||||
ss = s.next()
|
||||
def hamming_numbers():
|
||||
last = 1
|
||||
yield last
|
||||
|
||||
def p(n):
|
||||
def gen():
|
||||
x = n
|
||||
while True:
|
||||
yield x
|
||||
x *= n
|
||||
return gen()
|
||||
a,b,c = tee(hamming_numbers(), 3)
|
||||
|
||||
def pp(n, s):
|
||||
def gen():
|
||||
for x in (merge(s, chain([n], (n * y for y in fb)))):
|
||||
yield x
|
||||
r, fb = tee(gen())
|
||||
return r
|
||||
|
||||
def hamming(a, b = None):
|
||||
if not b:
|
||||
b = a + 1
|
||||
seq = (chain([1], pp(5, pp(3, p(2)))))
|
||||
return list(islice(seq, a - 1, b - 1))
|
||||
|
||||
print hamming(1, 21)
|
||||
print hamming(1691)[0]
|
||||
print hamming(1000000)[0]
|
||||
for n in merge((2*i for i in a), (3*i for i in b), (5*i for i in c)):
|
||||
if n != last:
|
||||
yield n
|
||||
last = n
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue