done 1 mobius

This commit is contained in:
Ivan I. Ovchinnikov 2023-02-13 13:27:26 +03:00
parent 9fb2fe18cc
commit 10a8cb858e
2 changed files with 58 additions and 6 deletions

View File

@ -10,7 +10,7 @@
-author("ivan-igorevich"). -author("ivan-igorevich").
%% API %% API
-export([is_prime/1, prime_factors/1, say/0]). -export([is_prime/1, prime_factors/1, is_square_multiple/1, find_square_multiples/2, say/1, start_time_check/0]).
%% region is_prime %% region is_prime
is_prime(N) when N < 2 -> is_prime(N) when N < 2 ->
@ -27,9 +27,9 @@ is_prime(N, I, Max) ->
true -> true ->
is_prime(N, I + 1, Max) is_prime(N, I + 1, Max)
end. end.
%% endregion %% endregion is_prime
%region factor %% region prime_factors
prime_factors(N) -> prime_factors(N) ->
prime_factors(N, 2, []). prime_factors(N, 2, []).
@ -42,7 +42,53 @@ prime_factors(X, N, Primes) ->
false -> false ->
[X | Primes] [X | Primes]
end. end.
%endregion %% endregion prime_factors
say() -> io:format("The result is: ~p~n", [prime_factors(111)]). %% region is_square_multiple
is_square_multiple(N)->
is_square_multiple_fun(prime_factors(N)).
is_square_multiple_fun([H | T]) ->
case lists:member(H, T) of
true -> true;
false -> is_square_multiple_fun(T)
end;
is_square_multiple_fun([]) ->
false.
%% endregion is_square_multiple
%% region nd_square_multiples
find_square_multiples(Count, MaxN) ->
find_square_multiples_fun(Count, MaxN, []).
find_square_multiples_fun(Count, CurrentNumber, FoundSquareMultipleNumber)
when length(FoundSquareMultipleNumber) == Count ->
CurrentNumber + 1;
find_square_multiples_fun(_, 2, _) ->
fail;
find_square_multiples_fun(Count, TestNumber, FoundSquareMultipleNumber) ->
case is_square_multiple(TestNumber) of
true -> FoundsNumber = FoundSquareMultipleNumber ++ [TestNumber];
false -> FoundsNumber = []
end,
find_square_multiples_fun(Count, TestNumber - 1, FoundsNumber).
%% endregion nd_square_multiples
for(0, _, _) -> [];
for(N, Term, Step) when N > Term ->
exec_time(N),
[Term | for(N - Step, Term, Step)].
start_time_check() ->
for(6, 4, 1).
exec_time(Depth) ->
Start = os:timestamp(),
say(Depth),
io:format("Depth = ~p, Time ~f sec~n", [Depth, (timer:now_diff(os:timestamp(), Start) / 1000)]).
say(Depth) -> io:format("The result is: ~p~n", [find_square_multiples(Depth, 30000)]).

View File

@ -10,7 +10,8 @@
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
mobius_test_() -> [ mobius_test_() -> [
{"Test is_prime", fun test_is_prime/0} {"Test is_prime", fun test_is_prime/0},
{"Test prime_factors", fun test_find_square_multiples/0}
]. ].
test_is_prime() -> test_is_prime() ->
@ -22,3 +23,8 @@ test_is_prime() ->
?assertNot(mobius:is_prime(60)), ?assertNot(mobius:is_prime(60)),
?assertNot(mobius:is_prime(112)), ?assertNot(mobius:is_prime(112)),
?assert(mobius:is_prime(113)). ?assert(mobius:is_prime(113)).
test_find_square_multiples() ->
?assertEqual(fail, mobius:find_square_multiples(3, 20)),
?assertEqual(48, mobius:find_square_multiples(3, 50)).