Search code examples
language-agnosticnlpcode-golfrosetta-stone

Code Golf: Number to Words


The code golf series seem to be fairly popular. I ran across some code that converts a number to its word representation. Some examples would be (powers of 2 for programming fun):

  • 2 -> Two
  • 1024 -> One Thousand Twenty Four
  • 1048576 -> One Million Forty Eight Thousand Five Hundred Seventy Six

The algorithm my co-worker came up was almost two hundred lines long. Seems like there would be a more concise way to do it.

Current guidelines:

  • Submissions in any programming language welcome (I apologize to PhiLho for the initial lack of clarity on this one)
  • Max input of 2^64 (see following link for words, thanks mmeyers)
  • Short scale with English output preferred, but any algorithm is welcome. Just comment along with the programming language as to the method used.

Solution

  • Lisp, using only standard functions:

    (format nil "~r" 1234) ==> "one thousand two hundred thirty-four"
    

    Bonus:

    (format nil "~@r" 1234)  ==> "MCCXXXIV"