Skip to content

Instantly share code, notes, and snippets.

@myokoym
Created August 17, 2012 16:56
Show Gist options
  • Save myokoym/3380571 to your computer and use it in GitHub Desktop.
Save myokoym/3380571 to your computer and use it in GitHub Desktop.
Problem042
class Integer
def triangle_number?
((Math.sqrt(8 * self + 1) - 1) / 2) % 1 == 0
end
end
alphabets = Hash[*("A".."Z").to_a.map.with_index {|c, i| [c, i + 1] }.flatten]
File.open("words.txt") {|f|
p f.read.chomp.gsub("\"", "").split(/,/).count {|w| w.split(//).inject(0) {|v, i| v + alphabets[i] }.triangle_number? }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment