Euler Solution 17

From ProgSoc Wiki

Jump to: navigation, search

Solutions for Problem 17

How many letters would be needed to write all the numbers in words from 1 to 1000?

Ruby by tomchristmas

Runtime: 33ms

total = 0

def ones(str)
    if str == "0"
       return 0
    elsif str == "1"
       return 3
    elsif str == "2"
       return 3
    elsif str == "3"
       return 5
    elsif str == "4"
       return 4
    elsif str == "5"
       return 4
    elsif str == "6"
       return 3
    elsif str == "7"
       return 5
    elsif str == "8"
       return 5
    elsif str == "9"
       return 4
    end
end

def tens(str)
    t = 0

    if str[0,1] == "1"
       if str[1,1] == "0"
          t += 3
       elsif str[1,1] == "1" 
          t += 6 # eleven
       elsif str[1,1] == "2" 
          t += 6 # twelve
       elsif str[1,1] == "3" 
          t += 8 # thirteen
       elsif str[1,1] == "4" 
          t += 8 # fourteen
       elsif str[1,1] == "5" 
          t += 7 # fifteen
       elsif str[1,1] == "6"  
          t += 7 # sixteen
       elsif str[1,1] == "7"      
          t += 9 # seventeen
       elsif str[1,1] == "8" 
          t += 8 # eighteen
       elsif str[1,1] == "9" 
          t += 8 # nineteen
       end
    elsif str[0,1] == "2" # twenty
       t += 6 + ones(str[1,1])
    elsif str[0,1] == "3" # thirty
       t += 6 + ones(str[1,1])
    elsif str[0,1] == "4" # forty
       t += 5 + ones(str[1,1])
    elsif str[0,1] == "5" # fifty
       t += 5 + ones(str[1,1])
    elsif str[0,1] == "6" # sixty
       t += 5 + ones(str[1,1])
    elsif str[0,1] == "7" # seventy
       t += 7 + ones(str[1,1])
    elsif str[0,1] == "8" # eighty
       t += 6 + ones(str[1,1])
    elsif str[0,1] == "9" # ninety
       t += 6 + ones(str[1,1])
    end

    return t
end

def hundreds(str)
    t = 7 + ones(str[0,1])
    
    if str[1,2] != "00"
       t += 3 # and
       if str[1,1] == "0"
          t += ones(str[2,1])
       else
          t += tens(str[1,2])
       end
    end
 
    return t
end

1.upto(1000){|x|
  str = x.to_s

  if str == "1000"
     total += 11
  elsif str.length == 3
     total += hundreds(str)
  elsif str.length == 2
     total += tens(str)
  elsif str.length == 1
     total += ones(str)
  end
}

puts total

Python by Althalus

Runtime 0.0

import time
start_time = time.time()

#This one could quite easily be worked out with pencil and paper...
#Yes, I realise some of my naming doesn't make much sense.
#words
one2nine = ['one','two','three','four','five','six','seven','eight','nine',]
ten2eleven = ['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
tens = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
others = ['hundred','and']

totalchars=0
hundred=0
#each word in one2nine occurs 9 times in a hundred. Once in each of these sets of 10: 0,20,30,40,50,60,70,80,90
for word in one2nine:
	hundred += len(word)*9
#ten2eleven occurs once. The teens break pattern 10
for word in ten2eleven:
	hundred += len(word)
#twenty to nintey occurs 10 times each. (21,22,23,24,25,26,27,28,29,...)
for word in tens:
	hundred += len(word)*10
#the above occurs 10 times in a thusand.
totalchars += hundred*10
#'and' occurs 99 times per hundred( one hundred, one hundred AND one...,
#*9 (not in first hundred, but every hundred after that)
#hundred occurs 100 times every hunded, but not first hundred. (*9)
totalchars += len(others[1])*99*9
totalchars += len(others[0])*100*9
#The numbers in one2ten will occur 100 times each in front of the word hundred.
for word in one2nine:
	totalchars += len(word)*100
#Then there is one(3) thousand(8)
totalchars +=3
totalchars +=8
print(totalchars)
run_time = time.time() - start_time
print (run_time)
Personal tools