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
|
||||
|
|
|
|||
|
|
@ -1,13 +1,25 @@
|
|||
from itertools import tee, chain, groupby, islice
|
||||
from heapq import merge
|
||||
from itertools import tee
|
||||
|
||||
def hamming_numbers():
|
||||
last = 1
|
||||
yield last
|
||||
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.
|
||||
|
||||
a,b,c = tee(hamming_numbers(), 3)
|
||||
def deferred_output():
|
||||
for i in output:
|
||||
yield i
|
||||
|
||||
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
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
38
Task/Hamming-numbers/Python/hamming-numbers-6.py
Normal file
38
Task/Hamming-numbers/Python/hamming-numbers-6.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from itertools import islice, chain, 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 p(n):
|
||||
def gen():
|
||||
x = n
|
||||
while True:
|
||||
yield x
|
||||
x *= n
|
||||
return gen()
|
||||
|
||||
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]
|
||||
Loading…
Add table
Add a link
Reference in a new issue