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


happy_numbers(L, Nb) :-
    % creation of the list
    length(L, Nb),
    % Process of this list
    get_happy_number(L, 1).
 
 
% the game is over
get_happy_number([], _).
 
% querying the newt happy_number
get_happy_number([H | T], N) :-
     N1 is N+1,
    (is_happy_number(N) -> 
        H = N, 
        get_happy_number(T, N1); 
        get_happy_number([H | T], N1)).
 
% we must memorized the numbers reached 
is_happy_number(N) :-
    is_happy_number(N, [N]).
 
% a number is happy when we get 1
is_happy_number(N, _L) :-
    get_next_number(N, 1), !.
 
% or when this number is not already reached !
is_happy_number(N, L) :-
    get_next_number(N, NN),
    \+member(NN, L),
    is_happy_number(NN, [NN | L]).
 
% Process of the next number from N
get_next_number(N, NewN) :-
    get_list_digits(N, LD),
    maplist(square, LD, L),
    sumlist(L, NewN).
 
get_list_digits(N, LD) :-
	number_chars(N, LCD),
	maplist(number_chars_, LD, LCD).
 
number_chars_(D, CD) :-
	number_chars(D, [CD]).
 
square(N, SN) :-
	SN is N * N.