Anonymous user
Miller–Rabin primality test: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, change indentations in program and output, simplified the genPrimes subroutine..
m (→{{header|Run BASIC}}: Fix opening comment) |
m (→{{header|REXX}}: added/changed whitespace and comments, change indentations in program and output, simplified the genPrimes subroutine..) |
||
Line 2,893:
=={{header|REXX}}==
With a '''K''' of '''1''', there seems to be a not uncommon number of failures, but
::: with a '''K ≥ 2''', the failures are
::: with a '''K ≥ 3''', the failures are rare as hen's teeth.
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.*/▼
<br>To make the program small, the true prime generator was coded to be small, but not particularly fast.
call genPrimes limit /*suspenders now, use a belt later ··· */
say "There are" primePi 'primes ≤' limit /*might as well crow a wee bit*/▼
primePi=#; @MRpt='Miller─Rabin primality test' /*save count of actual primes; literal.*/
say
say copies('─', 89)
mrp=0
if
if
if !.z then iterate
say '[K='a"] " z "isn't prime !" /*
end /*z*/
say ' for 1──►'limit", K="right(a,length(accur))',',
@MRpt "found" mrp 'primes {out of' primePi"}."
end /*a*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Miller_Rabin: procedure; parse arg n,k /*Miller─Rabin: A.K.A. Rabin─Miller.*/▼
▲Miller_Rabin: procedure; parse arg n,k
do k;
x=(
if x==1 | x==nL then iterate /*
do r=1 for s-1; x=(x*x)//n /*compute new X ≡ X² modulus N. */
if x==1 then return 0 /*if unity, it's definitely not prime.
if x==nL then leave /*if N-1, then it could be prime. */
end /*r*/ /* // is really REXX's ÷ remainder.*/
if x\==nL
return 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲return 1 /*coulda/woulda/shoulda be prime.*/
genPrimes: parse arg high; @.=0; !.=0; @.1=2; @.2=3; !.2=1; !.3=1; #=2
▲ if j//@.k==0 then iterate j /*the Greek way, in days of yore.*/
▲ end /*k*/ /*a useless comment, but hey!! */
▲ end /*j*/ /*this comment not left blank. */
'''output''' when using the input of: <tt> 10000 10 </tt>
<pre>
There are 1229 primes ≤ 10000
─────────────────────────────────────────────────────────────────────────────────────────
[K=2] 2701 isn't prime !
for 1──►10000, K= 2, Miller─Rabin primality test found 1230 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 2, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 3, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 4, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 5, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 6, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 7, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 8, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K= 9, Miller─Rabin primality test found 1229 primes {out of 1229}.
─────────────────────────────────────────────────────────────────────────────────────────
for 1──►10000, K=10, Miller─Rabin primality test found 1229 primes {out of 1229}.
</pre>
|