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


int List(810);          \list of numbers in a cycle
int Inx;                \index for List
include c:\cxpl\codes;
 
 
func HadNum(N);         \Return 'true' if number N is in the List
int N;
int I;
[for I:= 0 to Inx-1 do
        if N = List(I) then return true;
return false;
]; \HadNum
 
 
func SqDigits(N);       \Return the sum of the squares of the digits of N
int N;
int S, D;
[S:= 0;
while N do
        [N:= N/10;
        D:= rem(0);
        S:= S + D*D;
        ];
return S;
]; \SqDigits
 
 
int N0, N, C;
[N0:= 0;                \starting number
C:= 0;                  \initialize happy (starting) number counter
repeat  N:= N0;
        Inx:= 0;        \reset List index
        loop    [N:= SqDigits(N);
                if N = 1 then                   \happy number
                        [IntOut(0, N0);  CrLf(0);
                        C:= C+1;
                        quit;
                        ];
                if HadNum(N) then quit;         \if unhappy number then quit
                List(Inx):= N;                  \if neither, add it to the List
                Inx:= Inx+1;                    \ and continue the cycle
                ];
        N0:= N0+1;                              \next starting number
until   C=8;            \done when 8 happy numbers have been found
]