Skip to content

Instantly share code, notes, and snippets.

@seanbamforth
Last active August 29, 2015 14:09
Show Gist options
  • Save seanbamforth/1472b3230b945b1e8860 to your computer and use it in GitHub Desktop.
Save seanbamforth/1472b3230b945b1e8860 to your computer and use it in GitHub Desktop.
Happiness Kata
#test.rb
#happy number checking
def nextPlace(testNum)
testNum.to_s.each_char.inject(0) { |sum, c| sum + c.to_i**2 }
end
def is_happy?(testNum)
a = b = testNum
while (a!=1) && (b!=1) do
a = nextPlace(a)
b = nextPlace(nextPlace(b))
return false if (a==b)
end
true
end
l = []
(1..100000).each do |i|
l<<i if is_happy?(i)
end
puts l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment