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


-module(tasks).
-export([main/0]).
-import(lists, [map/2, member/2, sort/1, sum/1]).
 
is_happy(X, XS) ->
    if
	X == 1 ->
	    true;
	X < 1 ->
	    false;
	true ->
	    case member(X, XS) of
		true -> false;
		false ->
		    is_happy(sum(map(fun(Z) -> Z*Z end, 
				     [Y - 48 || Y <- integer_to_list(X)])),
			     [X|XS])
	    end
    end.
 
main(X, XS) ->
    if
	length(XS) == 8 ->
	    io:format("8 Happy Numbers: ~w~n", [sort(XS)]);
	true ->
	    case is_happy(X, []) of
		true -> main(X + 1, [X|XS]);
		false -> main(X + 1, XS)
	    end
    end.
main() ->
    main(0, []).
 