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


/*REXX program  computes  and  displays  a  specified  amount  of   happy   numbers.    */
parse arg limit .                                /*obtain optional argument from the CL.*/
if limit=='' | limit==","  then limit=8          /*Not specified?  Then use the default.*/
haps=0                                           /*count of the happy numbers  (so far).*/
 
  do n=1  while haps<limit;    @.=0;  q=n        /*search the integers starting at unity*/
         do  until q==1                          /*determine if   Q   is a happy number.*/
         s=0                                     /*prepare to add squares of digits.    */
                 do j=1  for length(q)           /*sum the squares of the decimal digits*/
                 s=s + substr(q, j, 1) **2       /*add the square  of  a  decimal digit.*/
                 end   /*j*/
 
         if @.s  then iterate n                  /*if already summed,   Q   is unhappy. */
         @.s=1;  q=s                             /*mark the sum as found;   try  Q  sum.*/
         end   /*until*/
  say n                                          /*display the number    (N  is happy). */
  haps=haps+1                                    /*bump the  count  of  happy numbers.  */
  end          /*n*/
                                                 /*stick a fork in it,  we're all done. */