Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
47
Task/Happy-numbers/Prolog/happy-numbers-1.pro
Normal file
47
Task/Happy-numbers/Prolog/happy-numbers-1.pro
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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.
|
||||
2
Task/Happy-numbers/Prolog/happy-numbers-2.pro
Normal file
2
Task/Happy-numbers/Prolog/happy-numbers-2.pro
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
?- happy_numbers(L, 8).
|
||||
L = [1,7,10,13,19,23,28,31].
|
||||
Loading…
Add table
Add a link
Reference in a new issue