user
**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:

1. Show the   first twenty   Hamming numbers.
2. Show the   1691st   Hamming number (the last one below   231).
3. 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).
assistant
(formerly Perl 6)

### Merge version

**Works with**: rakudo version 2015-11-04

The limit scaling is not *required*, but it cuts down on a bunch of unnecessary calculation.

```
my $limit = 32;

sub powers_of ($radix) { 1, |[\*] $radix xx * }

my @hammings = 
  (   powers_of(2)[^ $limit ]       X*
      powers_of(3)[^($limit * 2/3)] X* 
      powers_of(5)[^($limit * 1/2)]
   ).sort;

say @hammings[^20];
say @hammings[1690]; # zero indexed
```

Output:

```
(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36)
2125764000
```

### Iterative version

**Works with**: rakudo version 6.c

This version uses a lazy list, storing a maximum of two extra value from the highest index requested

```
my \Hammings := gather {
  my %i = 2, 3, 5 Z=> (Hammings.iterator for ^3);
  my %n = 2, 3, 5 Z=> 1 xx 3;

  loop {
    take my $n := %n{2, 3, 5}.min;

    for 2, 3, 5 -> \k {
      %n{k} = %i{k}.pull-one * k if %n{k} == $n;
    }
  }
}
  
say Hammings.[^20];
say Hammings.[1691 - 1];
say Hammings.[1000000 - 1];
```

Output:

```
(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36)
2125764000
519312780448388736089589843750000000000000000000000000000000000000000000000000000000
```