Problem 17

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 1

tens = [6,6,5,5,5,7,6,6]                 
AND = 3
ten = 3
hundred = 7
thousand = 8


_1_9 = [3,3,5,4,4,3,5,5,4]            
_11_19 = [6,6,8,8,7,7,9,8,8]                    
_20_99 = []
_100_999 = []


#21_99
for i in tens:
    _20_99 += [i]
    for j in _1_9:
        _20_99 += [i+j]
    
        
#100_999
for i in _1_9:
    _100_999 += [i+hundred]
    for j in _1_9:
        _100_999 += [i+hundred+AND+j]
        
    _100_999 += [i+hundred+AND+ten]
    for j in _11_19:
        _100_999 += [i+hundred+AND+j]
    for j in _20_99:
        _100_999 += [i+hundred+AND+j]
        

answer = 0
for i in _1_9:
    answer += i

answer += ten
for i in _11_19:
    answer += i
for i in _20_99:
    answer += i
for i in _100_999:
    answer += i

print(answer + 3 + thousand)   

The output is 21124.
This run:~0.