Compare commits
3 Commits
396650eec0
...
10a8cb858e
Author | SHA1 | Date |
---|---|---|
Ivan I. Ovchinnikov | 10a8cb858e | |
Ivan I. Ovchinnikov | 9fb2fe18cc | |
Ivan I. Ovchinnikov | 536dbf6f15 |
24
src/fib.erl
24
src/fib.erl
|
@ -1,5 +1,5 @@
|
|||
-module(fib).
|
||||
-export([fib_p/1]).
|
||||
-export([fib_p/1, fib_g/1, tail_fib/1, say/0, start_time_check/0]).
|
||||
|
||||
% Сопоставление с образцом
|
||||
fib_p(0) -> 0;
|
||||
|
@ -9,4 +9,24 @@ fib_p(N) -> fib_p(N - 1) + fib_p(N - 2).
|
|||
% Сторожевая последовательность
|
||||
fib_g(N) when N < 1 -> 0;
|
||||
fib_g(N) when N < 2 -> 1;
|
||||
fib_g(N) -> fib_g(N-1) + fib_g(N-2).
|
||||
fib_g(N) -> fib_g(N - 1) + fib_g(N - 2).
|
||||
|
||||
% Хвостовая рекурсия
|
||||
tail_fib(N) -> tfib(N, 0, 1).
|
||||
tfib(0, Result, Next) -> Result;
|
||||
tfib(N, Result, Next) -> tfib(N - 1, Next, Result + Next).
|
||||
|
||||
say() -> io:format("The result is: ~p~n", [tail_fib(10)]).
|
||||
|
||||
exec_time(Depth) ->
|
||||
Start = os:timestamp(),
|
||||
fib_p(Depth),
|
||||
io:format("Depth = ~p, Time ~f sec~n", [Depth, (timer:now_diff(os:timestamp(), Start) / 1000)]).
|
||||
|
||||
for(0, _, _) -> [];
|
||||
for(N, Term, Step) when N > 0 ->
|
||||
exec_time(N),
|
||||
[Term | for(N - Step, Term, Step)].
|
||||
|
||||
start_time_check() ->
|
||||
for(40, 20, 5).
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
%%%-------------------------------------------------------------------
|
||||
%%% @author ivan-igorevich
|
||||
%%% @copyright (C) 2023, self
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 13. Feb 2023 8:49 AM
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(mobius).
|
||||
-author("ivan-igorevich").
|
||||
|
||||
%% API
|
||||
-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 ->
|
||||
false;
|
||||
is_prime(N) ->
|
||||
is_prime(N, 2, trunc(math:sqrt(N)) + 1).
|
||||
|
||||
is_prime(_, Max, Max) ->
|
||||
true;
|
||||
is_prime(N, I, Max) ->
|
||||
if
|
||||
N rem I =:= 0 ->
|
||||
false;
|
||||
true ->
|
||||
is_prime(N, I + 1, Max)
|
||||
end.
|
||||
%% endregion is_prime
|
||||
|
||||
%% region prime_factors
|
||||
prime_factors(N) ->
|
||||
prime_factors(N, 2, []).
|
||||
|
||||
prime_factors(X, N, Primes) when X rem N == 0 ->
|
||||
prime_factors(trunc(X / N), 2, [N | Primes]);
|
||||
prime_factors(X, N, Primes) ->
|
||||
case N < math:sqrt(X) + 1 of
|
||||
true ->
|
||||
prime_factors(X, N + 1, Primes);
|
||||
false ->
|
||||
[X | Primes]
|
||||
end.
|
||||
%% endregion prime_factors
|
||||
|
||||
%% 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)]).
|
||||
|
|
@ -11,7 +11,8 @@
|
|||
|
||||
fib_test_() -> [
|
||||
{"Test fib_p", fun test_fib_p/0 },
|
||||
{"Test fib_g", fun test_fib_g/0 }
|
||||
{"Test fib_g", fun test_fib_g/0 },
|
||||
{"Test tail_fib", fun test_tail_fib/0 }
|
||||
].
|
||||
|
||||
test_fib_p() ->
|
||||
|
@ -39,3 +40,16 @@ test_fib_g() ->
|
|||
?assertEqual(21, fib:fib_g(8)),
|
||||
?assertEqual(34, fib:fib_g(9)),
|
||||
?assertEqual(55, fib:fib_g(10)).
|
||||
|
||||
test_tail_fib() ->
|
||||
?assertEqual(0, fib:tail_fib(0)),
|
||||
?assertEqual(1, fib:tail_fib(1)),
|
||||
?assertEqual(1, fib:tail_fib(2)),
|
||||
?assertEqual(2, fib:tail_fib(3)),
|
||||
?assertEqual(3, fib:tail_fib(4)),
|
||||
?assertEqual(5, fib:tail_fib(5)),
|
||||
?assertEqual(8, fib:tail_fib(6)),
|
||||
?assertEqual(13, fib:tail_fib(7)),
|
||||
?assertEqual(21, fib:tail_fib(8)),
|
||||
?assertEqual(34, fib:tail_fib(9)),
|
||||
?assertEqual(55, fib:tail_fib(10)).
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
%%%-------------------------------------------------------------------
|
||||
%%% @author ivan-igorevich
|
||||
%%% @copyright (C) 2023, self
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 13. Feb 2023 9:03 AM
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(mobius_test).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
mobius_test_() -> [
|
||||
{"Test is_prime", fun test_is_prime/0},
|
||||
{"Test prime_factors", fun test_find_square_multiples/0}
|
||||
].
|
||||
|
||||
test_is_prime() ->
|
||||
?assertNot(mobius:is_prime(1)),
|
||||
?assert(mobius:is_prime(2)),
|
||||
?assert(mobius:is_prime(3)),
|
||||
?assertNot(mobius:is_prime(58)),
|
||||
?assert(mobius:is_prime(59)),
|
||||
?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