Skip to content

Instantly share code, notes, and snippets.

@daviddavis
Created May 15, 2011 00:58
Show Gist options
  • Save daviddavis/972789 to your computer and use it in GitHub Desktop.
Save daviddavis/972789 to your computer and use it in GitHub Desktop.
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