Skip to main content

Euler 0017

The Problem:

WhatIf isyou were to write out a number as a word, you could then count the sumletters and generate a new number.  
For example: 1 becomes one which becomes 3.  
We use british convention which adds an 'and' between the hundreds and tens place. For example one hundred and forty-four.
How many letters are used in total to write out all of the digitsnumbers from 1 to 1000? We can exclude hyphens. 

Considerations and Approach:

There are a lot of 2^1000?

Considerations:

With pythonuniques we canneed triviallyto calculatekeep 2^1000track andof, but then we can convert itnumbers off of a set of rules.  

1 - one - 3  
2 - two - 3  
3 - three - 5  
4 - four - 4  
5 - five - 4  
6 - six - 3  
7 - seven - 5  
8 - eight - 5  
9 - nine - 4  
10 - ten - 3  
11 - eleven - 6  
12 - twelve - 6  
13 - thirteen - 8  
14 - fourteen - 8  
15 - fifteen - 7  
16 - sixteen - 7  
17 - seventeen - 9  
18 - eighteen - 8  
19 - nineteen - 8  
20 - twenty - 6  
30 - thirty - 6  
40 - forty - 5  
50 - fifty - 5  
60 - sixty - 5  
70 - seventy - 7  
80 - eighty - 6  
90 - ninety - 6  
and - 3  
thousand - 8  
hundred - 7  


1 through twenty have distinct numbers so we will need to akeep stringthat andinto sum its digits.

The Approach:

We calculate 2^1000 and then we can convert it to a string and sum its digits.account.


The Code:

powernumber_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}
number_end = 1000
number = 2**power

total_sum = 0
for digiti in str(number)range(1,number_end + 1):
    construction = ""
    hundred = False
    if int(i / 1000) > 0:
        total_sum += number_dict[int(digit)i / 1000)] #thousands place number
        #print(i,int(i / 1000))
        total_sum += 8 #the word thousand
    if int((i % 1000) / 100) > 0:
        #print(i,int((i % 1000) / 100))
        total_sum += number_dict[int((i % 1000) / 100)] #hundreds place number
        total_sum += 7 #the word hundred
        hundred = True
        construction += str(int((i % 1000) / 100)) + "hundred"
    if int((i % 100)) > 0:
        tens = int((i % 100)/10)*10
        ones = int((i % 100))
        ones_true = int(i%10)
        if hundred:
            construction += " and "
            total_sum += 3 #the word and
        if tens >= 20: #numbers greater than 20
            construction += str(tens)
            total_sum += number_dict[tens]
        if ones > 9 and tens < 20: #10 through 19
            construction += str(ones)
            total_sum += number_dict[ones]
        if ones_true > 0 and (ones < 10 or ones > 19): #ten through 19 are weird
            construction += str(ones_true)
            total_sum += number_dict[ones_true]
    #print(construction)

print(total_sum)