Since checking if number belongs to Hamming class is particular case of integer factorization problem then most trivial approach would be to try recursively factorize that number. For example - Hamming number test until 500-th integer

1
2
3
4
5
6
7
8

let rec IsHammingNumber n d =
match (n,d) with
| (1,_) -> true
| (_,_) when d > 5 -> false
| (_,_) -> match (n % d = 0) with
| true ->  IsHammingNumber (n/d) d
| false -> IsHammingNumber n (d+1)  in
[for x in 1..500 do if IsHammingNumber x 2 then yield x] |> printfn "%A"

But that may be in-efficient, just a jump start on problem...

By on 12/17/2010 11:26 AM ()