From: Bob Showalter Date: 2006-09-06T10:31:16+09:00 Subject: Re: [QUIZ] Happy Numbers (#93) I'm just posting my happy? method since it's a little different from the others. I opted for a recursive solution that records its results as it goes. I set the cache value to false before recursing; if the number winds up being happy, the true values are set as the recursion unwinds. class Integer # cache of happy true/false by number @@happy = Hash.new # sum of squares of digits def sosqod sum = 0 self.to_s.each_byte { |d| d -= ?0; sum += d * d } sum end # am I a happy number? def happy? return true if self == 1 return @@happy[self] if @@happy.include?(self) @@happy[self] = false @@happy[self] = self.sosqod.happy? end end