Data update

This commit is contained in:
Ingy döt Net 2025-02-27 18:35:13 -05:00
parent 8e4e15fa56
commit 72eb4943cb
1853 changed files with 35514 additions and 9441 deletions

View file

@ -0,0 +1,59 @@
function isProbablyPrime(n as *BigInteger, k as long) {
boolean T=true, F=false
=F
Zero=BigInteger("0")
One=BigInteger("1")
Two=BigInteger("2")
Method n, "compare", Two as c1
Method n, "modulus", Two as m2
method m2,"compare", zero as C2
if c1=0 or c2=0 then exit
with n, "toString" as ns$
s=0
Method n, "subtract", one as nn
d=nn
with d, "tostring" as dstr$
do
method d,"modulus", two as m2
method m2,"compare", zero as C
if c else exit
s++
method d, "divide", two as d
Always
z=len(ns$)
a=one
with a, "toString" as astr$
x=a
=T
for i=1 to k {
do
zs=""
for j=1 to len(ns$)
zs+=chr$(47+random(1,10))
next
a=bigInteger(zs)
method nn,"compare", a as C
method a,"compare", one as c1
until c=1 and c1>-1
method a, "modpow", d, n as x
method x,"compare", one as c1
if c1 else continue
method x,"compare", nn as c1
if c1 else continue
for r=1 to s {
method x, "modpow", two, n as x
method x,"compare", one as c1
if c1 else =F : break
method x,"compare", nn as c1
if c1 else exit
}
if c1 then =F: break
}
}
profiler
Print isProbablyPrime(BigInteger("5400349"), 5)=True
print timecount
profiler
a=BigInteger("5400349"): Method a, "isProbablyPrime", 5 as ret:Print ret
print timecount

View file

@ -0,0 +1,57 @@
uses System.Security.Cryptography;
function IsProbablePrime(source: biginteger; certainty: integer): boolean;
begin
if (source = 2) or (source = 3) then
begin result := true; exit end;
if (source < 2) or (source mod 2 = 0) then
begin result := false; exit end;
var d := source - 1;
var s := 0;
while d mod 2 = 0 do
begin
d := d div 2;
s += 1;
end;
var rng := RandomNumberGenerator.Create();
var bytes := new byte[source.ToByteArray.LongLength];
var a: biginteger;
loop certainty do
begin
repeat
rng.GetBytes(bytes);
a := new BigInteger(bytes);
until (a >= 2) and (a < source - 2);
var x := BigInteger.ModPow(a, d, source);
if (x = 1) or (x = source - 1) then continue;
for var r := 1 to s - 1 do
begin
x := BigInteger.ModPow(x, 2, source);
if x = 1 then
begin result := false; exit end;
if x = source - 1 then break;
end;
if x <> source - 1 then
begin result := false; exit end;
end;
result := true;
end;
begin
var data := |'4547337172376300111955330758342147474062293202868155909489',
'4547337172376300111955330758342147474062293202868155909393'|;
foreach var candidate in data do
isprobableprime(candidate.tobiginteger, 10).Println;
foreach var x in (900..1000) do
if isprobableprime(x, 10) then print(x);
end.

View file

@ -1,48 +0,0 @@
/*REXX program puts the Miller─Rabin primality test through its paces. */
parse arg limit times seed . /*obtain optional arguments from the CL*/
if limit=='' | limit=="," then limit= 1000 /*Not specified? Then use the default.*/
if times=='' | times=="," then times= 10 /* " " " " " " */
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); w= length(times) /*use absolute value of TIMES; get len.*/
call genP limit /*suspenders now, use a belt later ··· */
@MR= 'MillerRabin primality test' /*define a character literal for SAY. */
say "There are" # 'primes ' limit /*might as well display some stuff. */
say /* [↓] (skipping unity); show sep line*/
do a=2 to times; say copies('', 89) /*(skipping unity) do range of TIMEs.*/
p= 0 /*the counter of primes for this pass. */
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 /*well, we found another one, by gum! */
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 !*/
end /*z*/
say ' for 1'limit", K="right(a,w)',' @MR "found" p 'primes {out of' #"}."
end /*a*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: parse arg high; @.=0; @.1=2; @.2=3; !.=@.; !.2=1; !.3=1; #=2
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; !.j= 1 /*bump prime counter; add prime to the */
end /*j*/; return /*@. array; define a prime in !. array.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
M_Rt: procedure; parse arg n,k; d= n-1; nL=d /*Miller─Rabin: A.K.A. Rabin─Miller.*/
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.*/
do s=-1 while d//2==0; d= d % 2 /*keep halving until a zero remainder.*/
end /*while*/
do k; ?= random(2, nL) /* [↓] perform the DO loop K times.*/
x= ?**d // n /*X can get real gihugeic really fast.*/
if x==1 | x==nL then iterate /*First or penultimate? Try another pow*/
do s; x= x**2 // 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 REXX's division remainder*/
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.*/

View file

@ -1,90 +0,0 @@
include Settings
say version; say 'Miller-Rabin primality test'; say
numeric digits 1000
say '25 numbers of the form 2^n-1, mostly Mersenne primes'
say 'Up to about 25 digits deterministic, above probabilistic'
say
mm = '2 3 5 7 11 13 17 19 23 31 61 89 97 107 113 127 131 521 ',
|| '607 1279 2203 2281 2293 3217 3221'
do nn = 1 to 25
a = Word(mm,nn); b = 2**a-1; l = Length(b)
call Time('r'); p = IsPrime(b); e = Time('e')
if l > 20 then
b = Left(b,10)'...'Right(b,10)
select
when p = 0 then
p = 'not'
when l < 26 then
p = 'for sure'
otherwise
p = 'probable'
end
say '2^'a'-1' '=' b '('l' digits) is' p 'prime' '('Format(e,,3) 'seconds)'
end
return
IsPrime:
/* Is a number prime? */
procedure expose glob.
arg x
/* Low primes also used as deterministic witnesses */
w1 = ' 2 3 5 7 11 13 17 19 23 29 31 37 41 '
/* Fast values */
w = x
if Pos(' 'w' ',w1) > 0 then
return 1
if x//2 = 0 then
return 0
if x//3 = 0 then
return 0
if Right(x,1) = 5 then
return 0
/* Miller-Rabin primality test */
numeric digits 2*Length(x)
d = x-1; e = d
/* Reduce n-1 by factors of 2 */
do s = -1 while d//2 = 0
d = d%2
end
/* Thresholds deterministic witnesses */
w2 = '2047 1373653 25326001 3215031751 2152302898747 3474749660383 341550071728321 ',
|| '0 3825123056546413051 0 0 318665857834031151167461 3317044064679887385961981 '
w = Words(w2)
/* Up to 13 deterministic trials */
if x < Word(w2,w) then do
do k = 1 to w
if x < Word(w2,k) then
leave
end
end
/* or 3 probabilistic trials */
else do
w1 = ' '
do k = 1 to 3
r = Rand(2,e)/1; w1 = w1||r||' '
end
k = k-1
end
/* Algorithm using generated witnesses */
do k = 1 to k
a = Word(w1,k); y = Powermod(a,d,x)
if y = 1 then
iterate
if y = e then
iterate
do s
y = (y*y)//x
if y = 1 then
return 0
if y = e then
leave
end
if y <> e then
return 0
end
return 1
include Functions
include Numbers
include Abend

View file

@ -0,0 +1,29 @@
include Settings
say version; say 'Miller-Rabin primality test'; say
numeric digits 1000
say '25 numbers of the form 2^n-1, mostly Mersenne primes'
say 'Up to about 25 digits deterministic, above probabilistic'
say
mm = '2 3 5 7 11 13 17 19 23 31 61 89 97 107 113 127 131 521 ',
|| '607 1279 2203 2281 2293 3217 3221'
do nn = 1 to 25
a = Word(mm,nn); b = 2**a-1; l = Length(b)
call Time('r'); p = Prime(b); e = Time('e')
if l > 20 then
b = Left(b,10)'...'Right(b,10)
select
when p = 0 then
p = 'not'
when l < 26 then
p = 'for sure'
otherwise
p = 'probable'
end
say '2^'a'-1' '=' b '('l' digits) is' p 'prime' '('Format(e,,3) 'seconds)'
end
return
include Functions
include Numbers
include Abend