From Wikipedia, the free encyclopedia:

 A happy number is defined by the following process:
 Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not include   1.   


 Those numbers for which this process end in   1   are       happy   numbers,   
 while   those numbers   that   do   not   end in   1   are   unhappy   numbers. 


Task

Find and print the first   8   happy numbers.

Display an example of your output here on this page.



See also

   The OEIS entry:   The     happy numbers:   A007770
   The OEIS entry:   The unhappy numbers;   A031177


sum_dig_sq = proc (n: int) returns (int)
    sum_sq: int := 0
    while n > 0 do
        sum_sq := sum_sq + (n // 10) ** 2
        n := n / 10
    end
    return (sum_sq)
end sum_dig_sq
 
is_happy = proc (n: int) returns (bool)
    nn: int := sum_dig_sq(n)
    while nn ~= n cand nn ~= 1 do
        n := sum_dig_sq(n)
        nn := sum_dig_sq(sum_dig_sq(nn))
    end
    return (nn = 1)
end is_happy 
 
happy_numbers = iter (start, num: int) yields (int)
    n: int := start
    while num > 0 do
        if is_happy(n) then
            yield (n)
            num := num-1
        end
        n := n+1
    end
end happy_numbers
 
start_up = proc ()
    po: stream := stream$primary_output()
 
    for i: int in happy_numbers(1, 8) do
        stream$putl(po, int$unparse(i))
    end
end start_up 