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


/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/
limit	 = arg[0]                        /*get argument for  LIMIT.        */
say limit
if limit = null, limit ='' then limit=8  /*if not specified, set LIMIT to 8*/
haps	 = 0                             /*count of happy numbers so far.  */
 
loop n=1 while haps < limit              /*search integers starting at one.*/
  q=n                                    /*Q may or may not be "happy".    */
  a=0
 
  loop forever                           /*see if  Q  is a happy number.   */
    if q==1 then do                      /*if  Q  is unity, then it's happy*/
      haps = haps + 1                    /*bump the count of happy numbers.*/
      say n                              /*display the number.             */
      iterate n                          /*and then keep looking for more. */
    end
 
    sum=0                                /*initialize sum to zero.         */
 
    loop j=1 for q.length                /*add the squares of the numerals.*/
      sum = sum + q.substr(j,1) ** 2
    end
 
    if a[sum] then iterate n             /*if already summed, Q is unhappy.*/
    a[sum]=1                             /*mark the sum as being found.    */
    q=sum                                /*now, lets try the  Q  sum.      */
  end
end