hamming number | 1 | 2 | 3 | 4

1) compute

Build a table of 2i•3j•5k from i,j,k = 0 to n and sort it.

{def ham
 {lambda {:n}
  {S.sort <
   {S.map {{lambda {:n :i}
   {S.map {{lambda {:n :i :j} 
   {S.map {{lambda {:i :j :k} 
                   {* {pow 2 :i} {pow 3 :j} {pow 5 :k}}} :i :j}
   {S.serie 0 :n} } } :n :i}
   {S.serie 0 :n} } } :n}
   {S.serie 0 :n} }
}}} 
-> ham 

{def H {ham 30}}
-> H

{S.slice 0 19 {H}}
-> 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36

{S.get 1690 {H}} 
-> 2125764000

2) display

Display a hamming number as 2a•3b•5c

{def factor
 {def factor.r
  {lambda {:n :i}
   {if {> :i :n}
    then 
    else {if {= {% :n :i} 0}
    then :i {factor.r {/ :n :i} :i}
    else {factor.r :n {+ :i 1}} }}}}
 {lambda {:n}
  :n is the product of 1 {factor.r :n 2} }}
-> factor

{def asproductofpowers
 {def asproductofpowers.loop
  {lambda {:a :b :c :n}
   {if {= {S.first :n} 1}
    then 2{sup :a}•3{sup :b}•5{sup :c}
    else {asproductofpowers.loop
          {if {= {S.first :n} 2} then {+ :a 1} else :a}
          {if {= {S.first :n} 3} then {+ :b 1} else :b}
          {if {= {S.first :n} 5} then {+ :c 1} else :c}
          {W.rest :n} }
 }}}
 {lambda {:n}
  {asproductofpowers.loop 0 0 0 {S.reverse :n}}}}
-> asproductofpowers  

{factor 2125764000}
-> 2125764000 is the product of 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 

{asproductofpowers {factor 2125764000}}
-> 25•312•53

{S.map {lambda {:i} {div}:i: {S.get :i {H}} =
                    {asproductofpowers {factor {S.get :i {H}}}}} 
       {S.serie 0 19}}
->
0: 1 = 20•30•50
1: 2 = 21•30•50
2: 3 = 20•31•50
3: 4 = 22•30•50
4: 5 = 20•30•51
5: 6 = 21•31•50
6: 8 = 23•30•50
7: 9 = 20•32•50
8: 10 = 21•30•51
9: 12 = 22•31•50
10: 15 = 20•31•51
11: 16 = 24•30•50
12: 18 = 21•32•50
13: 20 = 22•30•51
14: 24 = 23•31•50
15: 25 = 20•30•52
16: 27 = 20•33•50
17: 30 = 21•31•51
18: 32 = 25•30•50
19: 36 = 22•32•50