Hamming numbers: Difference between revisions

Content added Content deleted
Line 645: Line 645:
===Alternate version using "Cyclic Iterators"===
===Alternate version using "Cyclic Iterators"===
The original author is Raymond Hettinger and the code was first published [http://code.activestate.com/recipes/576961/ here] under the MIT license. This implementation is quite memory efficient.
The original author is Raymond Hettinger and the code was first published [http://code.activestate.com/recipes/576961/ here] under the MIT license. This implementation is quite memory efficient.
<lang python>from itertools import tee, chain, groupby
<lang python>from itertools import tee, chain, groupby, islice
from heapq import merge
from heapq import merge


Line 669: Line 669:
output = no_repeats(combined) # eliminate duplicates
output = no_repeats(combined) # eliminate duplicates


return result</lang>
return result

print list(islice(raymonds_hamming(), 20))
print islice(raymonds_hamming(), 1689, 1690).next()
print islice(raymonds_hamming(), 999999, 1000000).next()</lang>


Results are the same as before.
Results are the same as before.

===Alternate version from the Java version===
===Alternate version from the Java version===
This is the fastest of the Python implementations, it uses a lot of memory.
This is the fastest of the Python implementations, it uses a lot of memory.