Happy Numbers (#93)

by Shane Emmons

Write a program that tells whether a given integer is happy. A happy number is found using the following process: Take the sum of the squares of its digits, and continue iterating this process until it yields 1, or produces an infinite loop.

For example the number 7:

7^2 = 49
4^2 + 9^2 = 97
9^2 + 7^2 = 130
1^2 + 3^2 + 0^2 = 10
1^2 + 0^2 = 1

If a number is not happy than it is obviously unhappy. Now that you have this program, what is the largest happy number you can find? What is the happiest number between 1 and 1,000,000. I define the happiest number as the smallest number that finds the most other happy numbers with it, i.e. 7 found four other numbers (49, 97, 130, and 10) making it a rank 4 in happiness.

If you find all these examples trivial, write you program so that it will find happy numbers in other bases such as base 2 or 16. From there you can extend the program so that it finds happy bases (other than 2 and 4). A happy bases is a base where all numbers are happy. Good luck.

Quiz Summary

I posted a link to some additional information early in the quiz discussion that expanded on the definition provided by the quiz:

Happy Number

According to that document, you can tell that a number is unhappy if the sum of the squares of the digits ever reaches 0, 4, 16, 20, 37, 42, 58, 89, or 145. That's really just a different way to find the repeat pattern mentioned in the quiz:

0: 0
4: 16 37 58 89 145 42 20 4
16: 37 58 89 145 42 20 4 16
20: 4 16 37 58 89 145 42 20
37: 58 89 145 42 20 4 16 37
42: 20 4 16 37 58 89 145 42
58: 89 145 42 20 4 16 37 58
89: 145 42 20 4 16 37 58 89
145: 42 20 4 16 37 58 89 145

Here's the code I used to generate the above list, which just loops over the sum of the squares until a repeat is found:

ruby
#!/usr/bin/env ruby -w

[0, 4, 16, 20, 37, 42, 58, 89, 145].each do |n|
print "#{n}: "

seen = {n => true}
loop do
sum = n.to_s.split("").inject(0) { |tot, d| tot + d.to_i ** 2 }

print "#{sum} "
if seen[sum]
puts
break
else
seen[sum] = true
n = sum
end
end
end

The advantage of using the list is that you don't need to wait for the pattern to start repeating and thus you find answers quicker.

Let's examine a solution that uses these numbers and another couple of optimizations. Here's my own code, strongly influenced by Simon Kroeger's solution:

ruby
#!/usr/bin/env ruby -w

UNHAPPY = [0, 4, 16, 20, 37, 42, 58, 89, 145].freeze

happy = Hash.new do |found, num|
digits = num.to_s.split("").sort.map { |d| d.to_i }.
delete_if { |d| d.zero? }
happiness = digits.inject(0) { |sum, d| sum + d * d }
found[num] = if happiness == 1
true
elsif UNHAPPY.include? happiness
false
else
found[happiness]
end
end

(1..100_000).each { |n| p n if happy[n] }

This is the standard Hash memoization pattern Ruby Quiz regulars are probably pretty familiar with by now. By creating a Hash and providing a block that can calculate the values from the keys, we ensure that Ruby will only run the code the first time it is needed. All other access is a simple Hash lookup and generally quite fast since Ruby's Hash is written in C.

The Hash block is where you will find all the hard work for this solution. The first step taken there is to convert the number into an Array of digits and you will find two more optimizations in this conversion. First note the final call to delete_if(). Zero squared is still zero and adding zero has no effect, so we can safely strip those out of the digits. That can take a number like 1,000,000 down to just the digit list of , skipping a fair amount of busy work.

The second optimization in here is the call to sort(). This consolidates what we need to store in the Hash a good deal. The numbers 123 and 321 both involve the same calculations, so we normalize digit order and take advantage of the ability to skip several calculations.

From there the block gets almost boring. A happiness rating is figured, which is just the sum of the digit squares. That rating is then checked for a known happy or unhappy value. If found, the Hash can set and return true or false. Otherwise the answer is determined by recursing to find the happiness of the sum.

This solution ends with a trivial iteration to print all happy numbers between one and 100,000.

My code just checked whether or not a given number is happy. The quiz mentioned other challenges and most people took them on. One such challenge involved finding out just how happy a number really is. Here's the start of some optimized code from Hans Fugal that does just that:

ruby
require 'set'

class Happy
def initialize
@happy_numbers = { 1 => 0 }
@unhappy_numbers = Set.new
end

# ...

You can see that Hans intends to track both happy and unhappy numbers. Happy numbers will be stored in a Hash with the number as the key and the value being the happiness rank for that number. Unhappy numbers will be a Set of numbers.

Note that you can't just use the keys for the happy numbers Hash to determine if a number is unhappy. Not being in that list may just mean the number hasn't been checked yet.

Here's the beginning of the method that does all the work:

ruby
# ...

def happy(x)
return true if @happy_numbers.has_key?(x)
return false if @unhappy_numbers.include?(x)

path = [x]
loop do
sum = 0
while x > 0
x, r = x.divmod(10)
sum += r**2
end

# ...

This method is used to check if a number is happy, but it squirrels away the happiness rank for the number as it finds the answer. You can see that it begins with checks that short-circuit the process when the result is already known. If the result is not yet known, the code enters a loop() to figure it out.

The path variable will eventually hold each step from the original number, to the squares sum that is known to be happy or unhappy. It begins with just what we currently know: the number itself.

The first bit of code in the loop() is a digit splitter and squares summation all-in-one. It divides the digits out and adds them to a running sum as it goes. This is quite a bit quicker than the multiple iterators used to do the same in my code.

Once we have a sum, it's time to check it for happiness:

ruby
# ...

if @unhappy_numbers.include?(sum)
return false
elsif @happy_numbers.has_key?(sum)
r = @happy_numbers[sum]
path.each_with_index do |x,i|
@happy_numbers[x] = r + path.size - i
end
return true
end

path.push sum

# ...

If the sum is unhappy, we know all we need to know and the result is immediately returned to the user.

If the sum is happy, we need to add all steps on the current path to the happy numbers Hash. Their rank is the rank of the sum we found plus their distance from the end of the current path. With that saved, true is returned to the calling code.

If we didn't find the number in either place it is just another step on the path and push() is called to reflect this.

Now we need the exit condition for the loop():

ruby
# ...

if [0, 1, 4, 16, 20, 37, 42, 58, 89, 145].include?(sum)
if sum == 1
s = path.size
path.each_with_index do |x,i|
@happy_numbers[x] = s - i - 1
end
return true
else
path.each do |x|
end
return false
end
end

x = sum
end
end

# ...

This final bit of code checks for the known happy and unhappy sums. If the number is happy, we again place each step in the path in the happy numbers Hash according to their distance from the end of the path. If the number is unhappy, all steps in the path are added to the unhappy numbers Set.

If the code makes it through all of that with no results, the current number is switched out for the squares sum and the code loop()s to find the answer.

The method we just digested saved the number's happiness rank, so we now need a way to get it back out:

ruby
# ...

def rank(x)
raise ArgumentError, "#{x} is unhappy." unless happy(x)
return @happy_numbers[x]
end
end

# ...

This method first ensures the number has been ranked with a call to happy(). Once it is known to be in the Hash, it's a simple lookup to locate and return a rank.

Here's the user interface code Hans included with the solution, which will give the happiness rank for any numbers passed via STDIN:

ruby
# ...

haphap = Happy.new
ARGF.each_line do |l|
l.scan(/\d+/) do |token|
x = token.to_i
if haphap.happy(x)
puts "#{x} is happy with rank #{haphap.rank(x)}"
end
end
end

Be sure and walk the other solutions. Many nice examples were given for finding happy bases. Daniel Martin even sent in a great NArray solution for that.

My thanks to all happy coders who got to play with happy numbers, allowing me to write this happy summary.

Tomorrow you all get a chance to earn double-O status, if your code is small enough and accurate...