1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators fry kernel lists lists.lazy locals math ;
4 IN: rosetta-code.hamming-lazy
6 ! http://rosettacode.org/wiki/Hamming_numbers#Factor
8 ! Hamming numbers are numbers of the form
9 ! H = 2^i * 3^j * 5^k where i, j, k >= 0
11 ! Hamming numbers are also known as ugly numbers and also
12 ! 5-smooth numbers (numbers whose prime divisors are less or equal
15 ! Generate the sequence of Hamming numbers, in increasing order.
18 ! 1. Show the first twenty Hamming numbers.
19 ! 2. Show the 1691st Hamming number (the last one below 231).
20 ! 3. Show the one millionth Hamming number (if the language – or
21 ! a convenient library – supports arbitrary-precision integers).
23 :: sort-merge ( xs ys -- result )
27 { [ x y < ] [ [ x ] [ xs cdr ys sort-merge ] lazy-cons ] }
28 { [ x y > ] [ [ y ] [ ys cdr xs sort-merge ] lazy-cons ] }
29 [ [ x ] [ xs cdr ys cdr sort-merge ] lazy-cons ]
32 :: hamming ( -- hamming )
35 h 2 3 5 [ '[ _ * ] lazy-map ] tri-curry@ tri