Archive for October 1st, 2010

Programming Praxis – Oban Numbers

October 1, 2010

In today’s Programming Praxis exercise, our task is to print a list of all Oban numbers (numbers that don’t have an o if you write them in words). Let’s get started, shall we?

Since any number higher than 999 will have either the word “thousand” or one of the -illions in it, we only have to implement spelling numbers less than a thousand. Spelling the numbers is a simple recursive algorithm. After spelling, we just take the ones that don’t have an o.

obans :: [Int]
obans = filter (notElem 'o' . spell) [1..999] where
    spell n | n <  20 = ones !! n
            | n < 100 = tens !! div n 10 ++ spell (mod n 10)
            | True    = spell (div n 100) ++ "hundred" ++ spell (mod n 100)
    ones = "" : words "one two three four five six seven eight \
                      \nine ten eleven twelve thirteen fourteen \
                      \fifteen sixteen seventeen eighteen nineteen"
    tens = "" : "" : words "twenty thirty forty fifty sixty \
                           \seventy eighty ninety"

Printing the numbers is trivial.

main :: IO ()
main = mapM_ print obans

If we instead say print $ length obans we see that there are indeed 454 Oban numbers, as there should be.