Anonymous user
Miller–Rabin primality test: Difference between revisions
m
→{{header|REXX}}: added/changed comments, optimized the SUSPENDERS subroutine.
m (→{{header|REXX}}: added/changed comments, optimized the SUSPENDERS subroutine.) |
|||
Line 2,246:
This would be in the same vein as: 3 is prime, 5 is prime, 7 is prime, all odd numbers are prime.
<lang rexx>/*REXX program puts the Miller-Rabin primality test through its paces.
if limit=='' | limit==',' then limit=1000 /*maybe assume LIMIT default.*/
▲arg limit accur . /*get some arguments (if any). */
if
tell= accur<0 /*show primes if K is negative.*/
▲numeric digits max(200,2*limit) /*we're dealing with some biggies*/
primePi=# /*save the count of (real) primes*/
say "There are" primePi 'primes ≤' limit /*might as well crow a wee bit
say /*nothing wrong with whitespace. */
do a=2 to accur
say copies('─',79) /*show separator for the eyeballs*/
mrp=0 /*prime counter for this pass. */
▲ do z=1 for limit /*now, let's get busy and crank. */
p=Miller_Rabin(z,a) /*invoke and pray... */
if p==0 then iterate
mrp=mrp+1 /*well, found another one, by gum*/
▲ if tell then say z, /*maybe should do a show & tell ?*/
'is prime according to Miller-Rabin primality test with K='a
if !.z\==0 then iterate▼
▲ if !.z\==0 then iterate
say '[K='a"] " z "isn't prime !" /*oopsy-doopsy & whoopsy-daisy!*/
end /*z*/
say 'for 1──►'limit", K="a', Miller-Rabin primality test found' mrp,
'primes {out of' primePi"}"
Line 2,281 ⟶ 2,276:
/*─────────────────────────────────────Rabin─Miller (also known as)─────*/
Miller_Rabin: procedure; parse arg n,k
if n==2
if n<2 | n//2==0 then return 0
d=n-1 /*just a temp variable for speed.*/
nL=n-1 /*saves a bit of time, down below*/
s=0 /*teeter-toter variable (see-saw)*/
do while d//2==0; d=d%2; s=s+1; end /*while d//2==0*/
x=(a**d) // n /*this number can get big fast. */
if x==1 | x==nL then iterate /*if so, try another power of A. */
do k▼
do r=1 for s-1; x=(x*x)//n /*compute new X ≡ X² modulus N.*/
if x=
if x==
if x\==nL
end
/*maybe it is, maybe it ain't ...*/
return 1 /*coulda/woulda/shoulda be prime.*/
/*──────────────────────────────────SUSPENDERS subroutine───────────────*/
suspenders: parse arg
do #=1 for words(_); p=word(_,#); @.#=p; s.#=p*p; !.p=1; end /*#*/
#=#-1 /*adjust the # of primes so far. */
do j
if j//3==0
do
#=#+1; @.#=j; s.#=j*j; !.j=1
end /*j*/ /*this comment not left blank. */
return /*whew! All done with the primes*/</lang>
{{out|Output when using the input of: <tt>10000 10</tt>}}
<pre style="height:30ex
There are 1229 primes ≤ 10000
|