# This Python file uses the following encoding: utf-8
# 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.
import time
start = time.time()
d = {0:0, 1:3, 2:3, 3:5, 4:4, 5:4, 6:3, 7:5, 8:5, 9:4, 10:3, 11:6, 12:6, 13:8, 14:8, 15:7, 16:7, 17:9, 18:8, 19:8, 20:6, 30:6, 40:5, 50:5, 60:5, 70:7, 80:6, 90:6, 100:7, 1000:8, 'and':3}
letters = 0
for i in xrange(1, 1001):
if i <= 20:
letters += d[i]
if 20 < i < 100:
letters += d[i - i%10] + d[i%10]
if 100 <= i < 1000:
letters += d[i/100] + d[100]
if i%100 != 0:
letters += d['and']
if i%100 <= 20:
letters += d[i%100]
else:
letters += d[i%100 - i%100%10] + d[i%10]
if i == 1000:
letters += d[1]
letters += d[1000]
print "%s letters in %s seconds" % (letters, time.time() - start)