Created
May 15, 2011 00:58
-
-
Save daviddavis/972789 to your computer and use it in GitHub Desktop.
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
def happy?(x) | |
seen = [] | |
while !(seen.include?(x)) | |
seen << x | |
x = x.to_s.chars.inject(0) { |sum, i| sum += (i.to_i * i.to_i) } | |
#puts "iteration: #{x}\n" | |
return true if x == 1 | |
end | |
return false | |
end | |
start = Time.now | |
(1..10000).each do |i| | |
puts "#{i} is " + (happy?(i) ? "" : "not ") + "happy\n" | |
end | |
puts "This program took #{Time.now - start} seconds" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment