Number Letter Countsproblem 17
If the numbers 1 to 5 is written out in Words:one, both, three, four, five, then there is 3 + 3 + 5 + 4 + 4 = Letters Used in total.
If all of the numbers from 1 to $ (one thousand) inclusive were written out in words, how many letters would is used?
Note: Do not count spaces or hyphens. For example, 342 (three hundred and Forty-two) contains, letters and (one hundred and fifteen) contains letters. The use of ' and ' when writing-out numbers are in compliance with British usage.
Python code:
dict={' 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, ' 0 ': 0}
def func1 (x):
return Dict[x]
def func2 (x):
K=len (x)
If k==2:
A=X[0]
B=X[1]
If a== ' 1 ':
return Dict[x]
Else
If b== ' 0 ':
Return dict[a+ ' 0 ']
Else
Return dict[a+ ' 0 ']+dict[b]
Else
return Func1 (x)
def func3 (x):
K=len (x)
If k==3:
Result=0
X0=X[0]
result+=dict[x0]+dict[' 100 ']
Fraction=int (X[1:3])
If fraction==0:
return result
Else
Result+=len (' and ')
RESULT+=FUNC2 (str (fraction))
return result
Else
return Func2 (x)
Result=0
For I in Range (1,1000):
RESULT+=FUNC3 (str (i))
Print (RESULT+11)
Time: <1s
Euler program (python) problem 17