Talk:Miller–Rabin primality test: Difference between revisions

(→‎Testing Composite Probability: Python merntions this too)
Line 15:
 
::It is not the absence on the task page of a function name or that the only function with the mentioned parameter names (n and k) is not doing the ''algorithm'' that is my problem, although both these things makes it more difficult. My problem is that I can not see any of the exported functions doing the task. Therefore I think that the Erlang solution might not be correct. [[User:bengt]]
 
== Erlang ==
 
Miller-Rabin primality test in Erlang uses a floating point power function and truncation to restore to integer.
This combination begins to fall short when the integer being tested is up to approximately 90,000,000. At these
large sizes, the primes found are correct but too many correct primes are not found. The following modifications
are helpful: an integer power function, power/2, will replace floating point power function, math:pow/2; in function
mr_series/4, math:pow/2 is replaced by power:power/2; in function pow_mod/3, the same replacement is made
and truncations are eliminated, also the expression E/2 is replaced by E div 2. One final change is to replace the
expression trunc(D/2) in find_ds/2 with the expression D div 2. By dogwood, 1/16/2014 @ 11:30pm.
 
-module(power).
-export([power/2]).
power(X, N) ->
power(X, N, 1).
power(X, N, Acc) ->
if
N /= 0 -> power(X, N - 1, X * Acc);
true -> Acc
end.
 
mr_series(N, A, D, S) when N rem 2 == 1 ->
Js = lists:seq(0, S),
lists:map(fun(J) -> pow_mod(A, power:power(2, J)*D, N) end, Js).
 
pow_mod(B, E, M) ->
case E of
0 -> 1;
_ -> case ((E rem 2) == 0) of
true -> (power:power(pow_mod(B, (E div 2), M), 2)) rem M;
false -> (B*pow_mod(B, E-1, M)) rem M
end
end.
 
find_ds(D, S) ->
case D rem 2 == 0 of
true ->
find_ds(D div 2, S+1);
false ->
{D, S}
end.
 
 
 
== Tcl ==
Anonymous user