Programming Praxis – Oban Numbers

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.


Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: