Project Euler Problem #17!

May 3rd, 2009
by Serinox

Problem #17 says:

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? 

This problem gives us the ability to use a semi recursive approach to generating the string versions of a number because in the English language we write numbers in a very specific way. Basically, with the exception of a few niche cases, we take the value of the number and break it into multiples of ten, such as 29 which we write as twenty-nine. Allowing us to create a function that takes the highest multiple of ten creates the string for that number, removes that digit from the number then passes that resulting value back into itself until we have the final value.

Solution provided in python:

def GenerateString(num):
    RetValue = ""
    if (num < 20):
        if (num == 1):
            RetValue = "one"
        if (num == 2):
            RetValue = "two"
        if (num == 3):
            RetValue = "three"
        if (num == 4):
            RetValue = "four"
        if (num == 5):
            RetValue = "five"
        if (num == 6):
            RetValue = "six"
        if (num == 7):
            RetValue = "seven"
        if (num == 8):
            RetValue = "eight"
        if (num == 9):
            RetValue = "nine"
        if (num == 10):
            RetValue = "ten"
        if (num == 11):
            RetValue = "eleven"
        if (num == 12):
            RetValue = "twelve"
        if (num == 13):
            RetValue = "thirteen"
        if (num == 14):
            RetValue = "fourteen"
        if (num == 15):
            RetValue = "fifteen"
        if (num == 16):
            RetValue = "sixteen"
        if (num == 17):
            RetValue = "seventeen"
        if (num == 18):
            RetValue = "eighteen"
        if (num == 19):
            RetValue = "nineteen"
    elif (num == 20):
        RetValue = "twenty"
    elif (num >= 21 and num <= 29):
        num = num - int(num/10) * 10
        RetValue = "twenty-" + GenerateString(num)
    elif (num == 30):
        RetValue = "thirty"
    elif (num >= 31 and num <= 39):
        num = num - int(num/10) * 10
        RetValue = "thirty-" + GenerateString(num)
    elif (num == 40):
        RetValue = "forty"
    elif (num >= 41 and num <= 49):
        num = num - int(num/10) * 10
        RetValue = "forty-" + GenerateString(num)
    elif (num == 50):
        RetValue = "fifty"
    elif (num >= 51 and num <= 59):
        num = num - int(num/10) * 10
        RetValue = "fifty-" + GenerateString(num)
    elif (num == 60):
        RetValue = "sixty"
    elif (num >= 61 and num <= 69):
        num = num - int(num/10) * 10
        RetValue = "sixty-" + GenerateString(num)
    elif (num == 70):
        RetValue = "seventy"
    elif (num >= 71 and num <= 79):
        num = num - int(num/10) * 10
        RetValue = "seventy-" + GenerateString(num)
    elif (num == 80):
        RetValue = "eighty"
    elif (num >= 81 and num <= 89):
        num = num - int(num/10) * 10
        RetValue = "eighty-" + GenerateString(num)
    elif (num == 90):
        RetValue = "ninety"
    elif (num >= 91 and num <= 99):
        num = num - int(num/10) * 10
        RetValue = "ninety-" + GenerateString(num)
    elif (num % 100==0 and num <> 1000):
        num = num /100
        RetValue = GenerateString(num) + "hundred"
    elif (num >= 101 and num <= 999):
        K = num /100
        num = num - K * 100
        RetValue = GenerateString(K) +" hundred and "+ GenerateString(num)
    elif (num == 1000):
        RetValue = "one thousand"
    return RetValue
result = 0
S = ""
for i in range(1,1001):
    S = GenerateString(i)
    for j in range(len(S)):
        if (S[j] <> ' ' and S[j] <> '-'):
            result = result + 1
print result

Posted in Project Euler | Comments (0)

No comments yet

Leave a Reply

You must be logged in to post a comment.