Anonymous user
Miller–Rabin primality test: Difference between revisions
m
→{{header|REXX}}: changed word in the REXX section header, added whitespace and optimization, added option (random seed) to allow for repeatable Miller-Rabin primality tests.
m (Reordered properly) |
m (→{{header|REXX}}: changed word in the REXX section header, added whitespace and optimization, added option (random seed) to allow for repeatable Miller-Rabin primality tests.) |
||
Line 4,040:
The '''K''' (above) is signified by the REXX variable '''times''' in the REXX program below.
<br>To make the program small, the
<lang rexx>/*REXX program puts the Miller─Rabin primality test through its paces. */
parse arg limit times seed .
if limit=='' | limit=="," then limit=
if times=='' | times=="," then times=
if datatype(seed, 'W') then call random ,,seed /*If seed specified, use it for RANDOM.*/
numeric digits max(200, 2*limit) /*we're dealing with some ginormous #s.*/
tell= times<0 /*display primes only if times is neg.*/
times= abs(times);
call genP limit /*suspenders now, use a belt later ··· */
@MR= 'Miller─Rabin primality test' /*define a character literal for SAY. */
Line 4,053 ⟶ 4,054:
say /* [↓] (skipping unity); show sep line*/
do a=2 to times; say copies('─', 89) /*(skipping unity) do range of TIMEs.*/
p= 0
do z=1 for limit /*now, let's get busy and crank primes.*/
if \M_Rt(z, a) then iterate /*Not prime? Then try another number.*/
p= p + 1
if tell then say z 'is prime according to' @MR "with K="a
if !.z then iterate
say '[K='a"] " z "isn't prime !" /*oopsy─doopsy and/or whoopsy─daisy !*/
Line 4,065 ⟶ 4,066:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: parse arg high;
do j=@.#+2 by 2 to high /*just examine odd integers from here. */
do k=2 while k*k<=j; if j//@.k==0 then iterate j; end /*k*/
#= # + 1; @.#= j;
end /*j*/; return /*@. array; define a prime in !. array.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
M_Rt: procedure; parse arg n,k; d= n-1; nL=d
if n==2 then return 1 /*special case of (the) even prime. */
if n<2 | n//2==0 then return 0 /*check for too low, or an even number.*/
end /*while*/▼
do s=-1 while
x=?**d // n /*X can get real gihugeic really fast.*/▼
do
if x==1 | x==nL then
if x
if x\==nL then return 0 /*nope, it ain't prime nohows, noway. */
end /*k*/ /*maybe it's prime, maybe it ain't ··· */
return 1 /*coulda/woulda/shoulda be prime; yup.*/</lang>
{{out|output|text= when using the input of: <tt> 10000 10 </tt>}}
|