Tag Archives: projecteuler

Euler Problem 17 in Ruby

After hearing about Ben’s motivation trouble with problem 17, I realized that I only had fun on that problem because I saw a rather fun algorithm. Since I’m rather proud of it, I’m going to show off the code here.

ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
hundred = "hundred"
and_ = "and"
thousand = "thousand"

sum = ""
1.upto(1000) do |i|
  curr = i
  out = ""
  if (curr > 999) then
    out += ones[curr/1000] + thousand
    curr = curr%1000
  end
  if (curr > 99) then
    out += ones[curr/100] + hundred
    curr = curr%100
    out += and_ if curr != 0
  end
  if (curr >= 20) then
    out += tens[curr/10]
    curr = curr%10
    out += ones[curr]
  else 
    out += ones[curr]
  end
  sum += out
end
puts sum.length

# Over 50 trials:
# Mean:	17.3689 ms
# Median: 16.5885 ms

I’m particularly proud of this code because I don’t have to convert the numbers into an array, and it’s blazing fast, even in ruby.