I am attempting to create a prime number factorizer, using fermats method.
This line generates an error
find_factors(A, B, FactorThis) when is_a_square(B) == true ->
call to local/imported function is_a_square/1 is illegal in guard
The only possible alternative I see to this implementation is to use some sort of case statement within the function. I was avoiding that, as it might screw up the tail recursion. I am a Erlang noob. What other ways are there to implement this functionality?
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).
Edited. fixed argument in call to calculate_new_b
added missing get_int_part_of_sqrts.
Here is another way to refactor around the issue.
Add the desired guard function as an argument at the caller. This turns it from a function with possible side effects, into true or false, which have no side effects. Then straight pattern matching will do the job.
Erlang deliberately restricts which functions you're allowed to call in guards. Here's a fairly recent discussion of the justification for this, its merits and drawbacks.
The only way around it is to use
case
. You can pretty easily rewrite this code to usecase
:Note that the above code is still properly tail-recursive.
(I modified your code a little to take out the parts that I'm guessing you meant not to have there)