首页 > 代码库 > 欧拉计划(python) problem 17
欧拉计划(python) problem 17
Number letter counts
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.
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
欧拉计划(python) problem 17