I am trying to create a prime factorizer using a farm method .
This line generates an error.
find_factors(A, B, FactorThis) when is_a_square(B) == true ->
calling local / imported function is_a_square / 1 is illegal in protection
The only possible alternative that I see in this implementation is to use some case argument in the function. I avoided this, as this could ruin the tail recursion. I'm Erlang noob. What other methods of implementing this function exist?
get_int_part_of_sqrt(N) -> trunc(math:sqrt(N)). is_a_square(N) -> get_int_part_of_sqrt(N) * get_int_part_of_sqrt(N) == N. calculate_new_b(A, FactorThis) -> NewB = trunc(abs((A * A) - FactorThis)), io:format("Calculate_new_b A^2 ~w- FT ~w= NB ~w ~n",[A*A,FactorThis,NewB]), find_factors(A, B, FactorThis) when is_a_square(B) == true -> io:format("find_factors true ~w ~w~n", [A, B]), {ok, A + get_int_part_of_sqrt(B), A - get_int_part_of_sqrt(B)}; find_factors(A, B, FactorThis) -> io:format("find_factors false ~w ~w~n", [A, B]), NewA = A + 1, NewB = calculate_new_b(NewA, FactorThis), find_factors(NewA, NewB, FactorThis).
Research1
Research2
Ed. fixed argument in call to calculate_new_b
Added missing get_int_part_of_sqrts.
Evilteach
source share