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


$ include "seed7_05.s7i";
 
const type: cacheType is hash [integer] boolean;
var cacheType: cache is cacheType.value;
 
const func boolean: happy (in var integer: number) is func
  result
    var boolean: isHappy is FALSE;
  local
    var bitset: cycle is bitset.value;
    var integer: newnumber is 0;
    var integer: cycleNum is 0;
  begin
    while number > 1 and number not in cycle do
      if number in cache then
        number := ord(cache[number]);
      else
        incl(cycle, number);
        newnumber := 0;
        while number > 0 do
          newnumber +:= (number rem 10) ** 2;
          number := number div 10;
        end while;
        number := newnumber;
      end if;
    end while;
    isHappy := number = 1;
    for cycleNum range cycle do
      cache @:= [cycleNum] isHappy;
    end for;
  end func;
 
const proc: main is func
  local
    var integer: number is 0;
  begin
    for number range 1 to 50 do
      if happy(number) then
        writeln(number);
      end if;
    end for;
  end func;