Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,32 @@
-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, []).

View file

@ -0,0 +1 @@
erl -run tasks main -run init stop -noshell

View file

@ -0,0 +1 @@
8 Happy Numbers: [1,7,10,13,19,23,28,31]

View file

@ -0,0 +1,18 @@
-module(tasks).
-export([main/0]).
main() -> io:format("~w ~n", [happy_list(1, 8, [])]).
happy_list(_, N, L) when length(L) =:= N -> lists:reverse(L);
happy_list(X, N, L) ->
Happy = is_happy(X),
if Happy -> happy_list(X + 1, N, [X|L]);
true -> happy_list(X + 1, N, L) end.
is_happy(1) -> true;
is_happy(4) -> false;
is_happy(N) when N > 0 ->
N_As_Digits = [Y - 48 || Y <- integer_to_list(N)],
is_happy(lists:foldl(fun(X, Sum) -> (X * X) + Sum end, 0, N_As_Digits));
is_happy(_) -> false.