wip1 mobius

This commit is contained in:
Ivan I. Ovchinnikov 2023-02-13 12:14:07 +03:00
parent 536dbf6f15
commit 9fb2fe18cc
2 changed files with 72 additions and 0 deletions

48
src/mobius.erl Normal file
View File

@ -0,0 +1,48 @@
%%%-------------------------------------------------------------------
%%% @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, say/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
%region factor
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
say() -> io:format("The result is: ~p~n", [prime_factors(111)]).

24
test/mobius_test.erl Normal file
View File

@ -0,0 +1,24 @@
%%%-------------------------------------------------------------------
%%% @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_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)).