Number letter counts


Fork me on GitHub
2014-09-12

Problem 017: Number letter counts

Description:

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.


Solution:
v
033544355436688779886
0366555766
$
$   v-1           <    v-1          <
>54*>:::1g"0"-\1p#^_54*>:::2g"0"-\2p|
v                       **25"d" 0 $$<
v                                              <
   v            $<   >:55+/2g\55+%           v
:              >:54*`|         >"d"/1g70     v
>0\>:!#v_:"d"\`| ^0g1<  >:"d"%!|        v    <
v      <       >:"}"8*-#^_$380v>:"d"/1g\ "d"%v
   ^                          <         <\3\7<
>+# \:# _+          \ 1-                      :|
                                 @.+_ #! #:\ #+<
Start
??
Pause
Reset
Output:
Stack:   (0)

Explanation:

There are only N kinds of numbers:

  • 0 - 20: Get the length from a precomputed list
  • 20 - 99: Get the length of the first word from a precomputed list and the length of the second word (second digit) from the previous point
  • 100 - 999: Get the length of the first word from a precomputed list and the length of the second and third word (second digit) from the previous point
  • 1000: Get the hard coded value

Note: Interestingly this program operates completely on the stack - only the initializing method sets a few "constant fields" to per-definied values


Interpreter steps: 179 076
Execution time (BefunExec): 47ms (3.81 MHz)
Program size: 48 x 15 (fully conform befunge-93)
Solution: 21124
Solved at: 2014-09-12



made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript