langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1 @@
|
|||
MR(n,k)=ispseudoprime(n,k);
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
sprp(n,b)={
|
||||
my(s = valuation(n-1, 2), d = Mod(b, n)^(n >> s));
|
||||
if (d == 1, return(1));
|
||||
for(i=1,s-1,
|
||||
if (d == -1, return(1));
|
||||
d = d^2;
|
||||
);
|
||||
d == -1
|
||||
};
|
||||
|
||||
MR(n,k)={
|
||||
for(i=1,k,
|
||||
if(!sprp(n,random(n-2)+2), return(0))
|
||||
);
|
||||
1
|
||||
};
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
A006945=[9, 2047, 1373653, 25326001, 3215031751, 2152302898747, 3474749660383, 341550071728321, 341550071728321, 3825123056546413051];
|
||||
Miller(n)={
|
||||
if (n%2 == 0, return(n == 2)); \\ Handle even numbers
|
||||
if (n < 3, return(0)); \\ Handle 0, 1, and negative numbers
|
||||
|
||||
if (n < 1<<64,
|
||||
\\ Feitsma
|
||||
for(i=1,#A006945,
|
||||
if (n < A006945[i], return(1));
|
||||
if(!sprp(n, prime(i)), return(0));
|
||||
);
|
||||
sprp(n,31)&sprp(n,37)
|
||||
,
|
||||
\\ Miller + Bach
|
||||
for(b=2,2*log(n)^2,
|
||||
if(!sprp(n, b), return(0))
|
||||
);
|
||||
1
|
||||
)
|
||||
};
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
# the expmod-function from: http://rosettacode.org/wiki/Modular_exponentiation
|
||||
sub expmod(Int $a is copy, Int $b is copy, $n) {
|
||||
my $c = 1;
|
||||
repeat while $b div= 2 {
|
||||
($c *= $a) %= $n if $b % 2;
|
||||
($a *= $a) %= $n;
|
||||
}
|
||||
$c;
|
||||
}
|
||||
|
||||
subset PrimeCandidate of Int where { $_ > 2 and $_ % 2 };
|
||||
|
||||
my Bool multi sub is-prime(Int $n, Int $k) { return False; }
|
||||
my Bool multi sub is-prime(2, Int $k) { return True; }
|
||||
my Bool multi sub is-prime(PrimeCandidate $n, Int $k) {
|
||||
my Int $d = $n - 1;
|
||||
my Int $s = 0;
|
||||
|
||||
while $d %% 2 {
|
||||
$d div= 2;
|
||||
$s++;
|
||||
}
|
||||
|
||||
for (2 ..^ $n).pick($k) -> $a {
|
||||
my $x = expmod($a, $d, $n);
|
||||
|
||||
# one could just write "next if $x == 1 | $n - 1"
|
||||
# but this takes much more time in current rakudo/nom
|
||||
next if $x == 1 or $x == $n - 1;
|
||||
|
||||
for 1 ..^ $s {
|
||||
$x = $x ** 2 mod $n;
|
||||
return False if $x == 1;
|
||||
last if $x == $n - 1;
|
||||
}
|
||||
return False if $x !== $n - 1;
|
||||
}
|
||||
|
||||
return True;
|
||||
}
|
||||
|
||||
say (1..1000).grep({ is-prime($_, 10) }).join(", ");
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
Enumeration
|
||||
#Composite
|
||||
#Probably_prime
|
||||
EndEnumeration
|
||||
|
||||
Procedure Miller_Rabin(n, k)
|
||||
Protected d=n-1, s, x, r
|
||||
If n=2
|
||||
ProcedureReturn #Probably_prime
|
||||
ElseIf n%2=0 Or n<2
|
||||
ProcedureReturn #Composite
|
||||
EndIf
|
||||
While d%2=0
|
||||
d/2
|
||||
s+1
|
||||
Wend
|
||||
While k>0
|
||||
k-1
|
||||
x=Int(Pow(2+Random(n-4),d))%n
|
||||
If x=1 Or x=n-1: Continue: EndIf
|
||||
For r=1 To s-1
|
||||
x=(x*x)%n
|
||||
If x=1: ProcedureReturn #Composite: EndIf
|
||||
If x=n-1: Break: EndIf
|
||||
Next
|
||||
If x<>n-1: ProcedureReturn #Composite: EndIf
|
||||
Wend
|
||||
ProcedureReturn #Probably_prime
|
||||
EndProcedure
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
input "Input a number:";n
|
||||
input "Input test:";k
|
||||
|
||||
test = millerRabin(n,k)
|
||||
if test = 0 then
|
||||
print "Probably Prime"
|
||||
else
|
||||
print "Composite"
|
||||
end if
|
||||
wait
|
||||
|
||||
' ----------------------------------------
|
||||
' Returns
|
||||
' Composite = 1
|
||||
' Probably Prime = 0
|
||||
' ----------------------------------------
|
||||
FUNCTION millerRabin(n, k)
|
||||
if n = 2 then
|
||||
millerRabin = 0 'probablyPrime
|
||||
goto [funEnd]
|
||||
end if
|
||||
|
||||
if n mod 2 = 0 or n < 2 then
|
||||
millerRabin = 1 'composite
|
||||
goto [funEnd]
|
||||
end if
|
||||
|
||||
d = n - 1
|
||||
while d mod 2 = 0
|
||||
d = d / 2
|
||||
s = s + 1
|
||||
wend
|
||||
|
||||
while k > 0
|
||||
k = k - 1
|
||||
x = (int(rnd(1) * (n-4))^d) mod n
|
||||
if x = 1 or x = n-1 then
|
||||
for r=1 To s-1
|
||||
x =(x * x) mod n
|
||||
if x=1 then
|
||||
millerRabin = 1 ' composite
|
||||
goto [funEnd]
|
||||
end if
|
||||
if x = n-1 then exit for
|
||||
next r
|
||||
|
||||
if x<>n-1 then
|
||||
millerRabin = 1 ' composite
|
||||
goto [funEnd]
|
||||
end if
|
||||
end if
|
||||
wend
|
||||
[funEnd]
|
||||
END FUNCTION
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
open LargeInt;
|
||||
|
||||
val mr_iterations = Int.toLarge 20;
|
||||
val rng = Random.rand (557216670, 13504100); (* arbitrary pair to seed RNG *)
|
||||
|
||||
fun expmod base 0 m = 1
|
||||
| expmod base exp m =
|
||||
if exp mod 2 = 0
|
||||
then let val rt = expmod base (exp div 2) m;
|
||||
val sq = (rt*rt) mod m
|
||||
in if sq = 1
|
||||
andalso rt <> 1 (* ignore the two *)
|
||||
andalso rt <> (m-1) (* 'trivial' roots *)
|
||||
then 0
|
||||
else sq
|
||||
end
|
||||
else (base*(expmod base (exp-1) m)) mod m;
|
||||
|
||||
(* arbitrary precision random number [0,n) *)
|
||||
fun rand n =
|
||||
let val base = Int.toLarge(valOf Int.maxInt)+1;
|
||||
fun step r lim =
|
||||
if lim < n then step (Int.toLarge(Random.randNat rng) + r*base) (lim*base)
|
||||
else r mod n
|
||||
in step 0 1 end;
|
||||
|
||||
fun miller_rabin n =
|
||||
let fun trial n 0 = true
|
||||
| trial n t = let val a = 1+rand(n-1)
|
||||
in (expmod a (n-1) n) = 1
|
||||
andalso trial n (t-1)
|
||||
end
|
||||
in trial n mr_iterations end;
|
||||
|
||||
fun trylist label lst = (label, ListPair.zip (lst, map miller_rabin lst));
|
||||
|
||||
trylist "test the first six Carmichael numbers"
|
||||
[561, 1105, 1729, 2465, 2821, 6601];
|
||||
|
||||
trylist "test some known primes"
|
||||
[7369, 7393, 7411, 27367, 27397, 27407];
|
||||
|
||||
(* find ten random 30 digit primes (according to Miller-Rabin) *)
|
||||
let fun findPrime trials = let val t = trials+1;
|
||||
val n = 2*rand(500000000000000000000000000000)+1
|
||||
in if miller_rabin n
|
||||
then (n,t)
|
||||
else findPrime t end
|
||||
in List.tabulate (10, fn e => findPrime 0) end;
|
||||
Loading…
Add table
Add a link
Reference in a new issue