Hamming numbers are numbers of the form  

    H = 2i × 3j × 5k
           where 
     i,  j,  k  ≥  0 

Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5).



Task

Generate the sequence of Hamming numbers, in increasing order.   In particular:

 Show the   first twenty   Hamming numbers.
 Show the   1691st   Hamming number (the last one below   231).
 Show the   one millionth   Hamming number (if the language – or a convenient library – supports arbitrary-precision integers).


Related tasks

 Humble numbers
 N-smooth numbers


References

 Wikipedia entry:   Hamming numbers     (this link is re-directed to   Regular number).
 Wikipedia entry:   Smooth number
 OEIS entry:   A051037   5-smooth   or   Hamming numbers
 Hamming problem from Dr. Dobb's CodeTalk (dead link as of Sep 2011; parts of the thread here and here).


 
Module hamming_long {
	function hamming(l as long, &h(),&last()) {
		l=if(l<1->1&, l)
		long oldlen=len(h())
		if oldlen<l then dim h(l) else =h(l-1): exit 
		def long  i, j, k, n, m, x2, x3, x5, ll
		stock last(0) out x2,x3,x5,i,j,k
		n=oldlen : ll=l-1
		{	m=x2
			if m>x3 then m=x3
			if m>x5 then m=x5
			h(n)=m
			if n>=1690 then =h(n):break
			if m=x2 then i++:x2=2&*h(i)
			if m=x3 then j++:x3=3&*h(j)
			if m=x5 then k++:x5=5&*h(k)
			if n<ll then n++: loop
		}
		stock last(0) in x2,x3,x5,i,j,k
		=h(ll)
	}
	dim h(1)=1&, last()
	def long i
	const nl$={
	}
	document doc$
	last()=(2&,3&,5&,0&,0&,0&)
	for i=1 to 20
		Doc$=format$("{0::-10} {1::-10}", i, hamming(i,&h(), &last()))+nl$
	next i
	i=1691
	Doc$=format$("{0::-10} {1::-10}", i, hamming(i,&h(), &last()))+nl$
	print #-2,Doc$
	clipboard Doc$
}
 hamming_long
 