Jump to content

Hamming numbers: Difference between revisions

→‎"Cyclical Iterators": upd: since 3.6 the tee() is efficient, sharing
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(→‎"Cyclical Iterators": upd: since 3.6 the tee() is efficient, sharing)
Line 9,380:
The original author is Raymond Hettinger and the code was first published [http://code.activestate.com/recipes/576961/ here] under the MIT license. Uses iterators dubbed "cyclical" in a sense that they are referring back (explicitly, with <code>p2, p3, p5</code> iterators) to the previously produced values, same as the above versions (through indices into shared storage) and the classic [[#Haskell|Haskell]] version (implicitly timed by lazy evaluation).
 
Memory is efficiently maintained automatically by the <code>tee</code> function for each of the three generator expressions, i.e. only that much is maintained as needed to produce the next value (although, for Python versions older than 3.6 it looks like the storage is not shared so three copies are maintained implicitly there -- whereas for 3.6 and up the storage <i>is</i> shared between the returned iterators, so only a single underlying FIFO queue is maintained, according to the [https://docs.python.org/3.6/library/itertools.html#itertools.tee documentation]).
<syntaxhighlight lang="python">from itertools import tee, chain, groupby, islice
from heapq import merge
Line 9,467:
print hamming(1691)[0]
print hamming(1000000)[0]</syntaxhighlight>
 
=={{header|QBasic}}==
{{works with|QBasic|1.1}}
751

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.