Hamming numbers
Haskell, 101 97 92+|n| characters
h=1:m 2h&m 3h&m 5h
m=map.(*)
c@(a:b)&o@(m:n)|a<m=a:b&o|a>m=m:c&n|0<1=a:b&n
main=print$take 1000000h
Computes the full million in 3.7s on the machine I tested on (variably more if you actually want the output stored)
Ungolfed:
-- print out the first million Hamming numbers
main = print $ take 1000000 h
-- h is the entire Hamming sequence.
-- It starts with 1; for each number in the
-- sequence, 2n, 3n and 5n are also in.
h = 1 : (m 2 h) & (m 3 h) & (m 5 h)
-- helper: m scales a list by a constant factor
m f xs = map (f*) xs
-- helper: (&) merges two ordered sequences
a@(ha:ta) & b@(hb:tb)
| ha < hb = ha : ta & b
| ha > hb = hb : a & tb
| otherwise = ha : ta & tb
All Haskell is notoriously good at: defining a list as a lazy function of itself, in a way that actually works.
Python 181 Characters
h=[]
h.append(1)
n=input()
i=j=k=0
while n:
print h[-1]
while h[i]*2<=h[-1]:
i+=1
while h[j]*3<=h[-1]:
j+=1
while h[k]*5<=h[-1]:
k+=1
h.append(min(h[i]*2,h[j]*3,h[k]*5))
n-=1
APL (Dyalog Classic), 34 23 bytes
{⍺⍴{⍵[⍋⍵]}∪,⍵∘.×⍳5}⍣≡∘1
Try it online!
TIO throws a WS FULL error for \$n = 1000000\$, but Dyalog on my laptop runs in about 45 seconds, not counting the scrolling to display numbers.
{⍺⍴{⍵[⍋⍵]}∪,⍵∘.×⍳5}⍣≡∘1 Monadic function:
{⍺⍴{⍵[⍋⍵]}∪,⍵∘.×⍳5} Define the following helper function g(⍺,⍵):
⍵∘.×⍳5 Make a multiplication table between ⍵ and (1 2 3 4 5).
(Including 4 is unnecessary but saves bytes.)
, Flatten the table into an array.
∪ Keep unique elements.
{⍵[⍋⍵]} Grade up the array and access it at those indices.
(This is the APL idiom to sort an array.)
⍺⍴ Keep the first ⍺ elements; pad by repeating the array.
{⍺⍴{⍵[⍋⍵]}∪,⍵∘.×⍳5}⍣≡ Repeatedly apply g with some fixed left argument
until a fixed point is reached.
At this point we have a dyadic function that takes
n on the left and the starting value on the right,
and returns multiples of the n Hamming numbers.
∘1 Fix 1 as the right argument.