Created
August 17, 2012 16:56
-
-
Save myokoym/3380571 to your computer and use it in GitHub Desktop.
Problem042
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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