Last active
August 29, 2015 14:09
-
-
Save seanbamforth/1472b3230b945b1e8860 to your computer and use it in GitHub Desktop.
Happiness Kata
This file contains hidden or 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
#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