done 1 mobius
This commit is contained in:
parent
9fb2fe18cc
commit
10a8cb858e
|
@ -10,7 +10,7 @@
|
|||
-author("ivan-igorevich").
|
||||
|
||||
%% 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
|
||||
is_prime(N) when N < 2 ->
|
||||
|
@ -27,9 +27,9 @@ is_prime(N, I, Max) ->
|
|||
true ->
|
||||
is_prime(N, I + 1, Max)
|
||||
end.
|
||||
%% endregion
|
||||
%% endregion is_prime
|
||||
|
||||
%region factor
|
||||
%% region prime_factors
|
||||
prime_factors(N) ->
|
||||
prime_factors(N, 2, []).
|
||||
|
||||
|
@ -42,7 +42,53 @@ prime_factors(X, N, Primes) ->
|
|||
false ->
|
||||
[X | Primes]
|
||||
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 find_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 find_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)]).
|
||||
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
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() ->
|
||||
|
@ -22,3 +23,8 @@ test_is_prime() ->
|
|||
?assertNot(mobius:is_prime(60)),
|
||||
?assertNot(mobius:is_prime(112)),
|
||||
?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)).
|
||||
|
|
Loading…
Reference in New Issue