June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,26 @@
(defn gcd [a b]
(if (zero? b)
a
(recur b (mod a b))))
(defn lcm [a b]
(/ (* a b) (gcd a b)))
(def NaN (Math/log -1))
(defn ord' [a [p e]]
(let [m (imath/expt p e)
t (* (quot m p) (dec p))]
(loop [dv (factor/divisors t)]
(let [d (first dv)]
(if (= (mmath/expm a d m) 1)
d
(recur (next dv)))))))
(defn ord [a n]
(if (not= (gcd a n) 1)
NaN
(->>
(factor/factorize n)
(map (partial ord' a))
(reduce lcm))))

View file

@ -0,0 +1,106 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class MultiplicativeOrder {
private static final BigInteger ONE = BigInteger.ONE;
private static final BigInteger TWO = BigInteger.valueOf(2);
private static final BigInteger THREE = BigInteger.valueOf(3);
private static final BigInteger TEN = BigInteger.TEN;
private static class PExp {
BigInteger prime;
long exp;
PExp(BigInteger prime, long exp) {
this.prime = prime;
this.exp = exp;
}
}
private static void moTest(BigInteger a, BigInteger n) {
if (!n.isProbablePrime(20)) {
System.out.println("Not computed. Modulus must be prime for this algorithm.");
return;
}
if (a.bitLength() < 100) System.out.printf("ord(%s)", a);
else System.out.print("ord([big])");
if (n.bitLength() < 100) System.out.printf(" mod %s ", n);
else System.out.print(" mod [big] ");
BigInteger mob = moBachShallit58(a, n, factor(n.subtract(ONE)));
System.out.println("= " + mob);
}
private static BigInteger moBachShallit58(BigInteger a, BigInteger n, List<PExp> pf) {
BigInteger n1 = n.subtract(ONE);
BigInteger mo = ONE;
for (PExp pe : pf) {
BigInteger y = n1.divide(pe.prime.pow((int) pe.exp));
long o = 0;
BigInteger x = a.modPow(y, n.abs());
while (x.compareTo(ONE) > 0) {
x = x.modPow(pe.prime, n.abs());
o++;
}
BigInteger o1 = BigInteger.valueOf(o);
o1 = pe.prime.pow(o1.intValue());
o1 = o1.divide(mo.gcd(o1));
mo = mo.multiply(o1);
}
return mo;
}
private static List<PExp> factor(BigInteger n) {
List<PExp> pf = new ArrayList<>();
BigInteger nn = n;
Long e = 0L;
while (!nn.testBit(e.intValue())) e++;
if (e > 0L) {
nn = nn.shiftRight(e.intValue());
pf.add(new PExp(TWO, e));
}
BigInteger s = sqrt(nn);
BigInteger d = THREE;
while (nn.compareTo(ONE) > 0) {
if (d.compareTo(s) > 0) d = nn;
e = 0L;
while (true) {
BigInteger[] qr = nn.divideAndRemainder(d);
if (qr[1].bitLength() > 0) break;
nn = qr[0];
e++;
}
if (e > 0L) {
pf.add(new PExp(d, e));
s = sqrt(nn);
}
d = d.add(TWO);
}
return pf;
}
private static BigInteger sqrt(BigInteger n) {
BigInteger b = n;
while (true) {
BigInteger a = b;
b = n.divide(a).add(a).shiftRight(1);
if (b.compareTo(a) >= 0) return a;
}
}
public static void main(String[] args) {
moTest(BigInteger.valueOf(37), BigInteger.valueOf(3343));
BigInteger b = TEN.pow(100).add(ONE);
moTest(b, BigInteger.valueOf(7919));
b = TEN.pow(1000).add(ONE);
moTest(b, BigInteger.valueOf(15485863));
b = TEN.pow(10000).subtract(ONE);
moTest(b, BigInteger.valueOf(22801763489L));
moTest(BigInteger.valueOf(1511678068), BigInteger.valueOf(7379191741L));
moTest(BigInteger.valueOf(3047753288L), BigInteger.valueOf(2257683301L));
}
}

View file

@ -0,0 +1,94 @@
// version 1.2.10
import java.math.BigInteger
val bigOne = BigInteger.ONE
val bigTwo = 2.toBigInteger()
val bigThree = 3.toBigInteger()
val bigTen = BigInteger.TEN
class PExp(val prime: BigInteger, val exp: Long)
fun moTest(a: BigInteger, n: BigInteger) {
if (!n.isProbablePrime(20)) {
println("Not computed. Modulus must be prime for this algorithm.")
return
}
if (a.bitLength() < 100) print("ord($a)") else print("ord([big])")
if (n.bitLength() < 100) print(" mod $n ") else print(" mod [big] ")
val mob = moBachShallit58(a, n, factor(n - bigOne))
println("= $mob")
}
fun moBachShallit58(a: BigInteger, n: BigInteger, pf: List<PExp>): BigInteger {
val n1 = n - bigOne
var mo = bigOne
for (pe in pf) {
val y = n1 / pe.prime.pow(pe.exp.toInt())
var o = 0L
var x = a.modPow(y, n.abs())
while (x > bigOne) {
x = x.modPow(pe.prime, n.abs())
o++
}
var o1 = o.toBigInteger()
o1 = pe.prime.pow(o1.toInt())
o1 /= mo.gcd(o1)
mo *= o1
}
return mo
}
fun factor(n: BigInteger): List<PExp> {
val pf = mutableListOf<PExp>()
var nn = n
var e = 0L
while (!nn.testBit(e.toInt())) e++
if (e > 0L) {
nn = nn shr e.toInt()
pf.add(PExp(bigTwo, e))
}
var s = bigSqrt(nn)
var d = bigThree
while (nn > bigOne) {
if (d > s) d = nn
e = 0L
while (true) {
val (q, r) = nn.divideAndRemainder(d)
if (r.bitLength() > 0) break
nn = q
e++
}
if (e > 0L) {
pf.add(PExp(d, e))
s = bigSqrt(nn)
}
d += bigTwo
}
return pf
}
fun bigSqrt(n: BigInteger): BigInteger {
var b = n
while (true) {
val a = b
b = (n / a + a) shr 1
if (b >= a) return a
}
}
fun main(args: Array<String>) {
moTest(37.toBigInteger(), 3343.toBigInteger())
var b = bigTen.pow(100) + bigOne
moTest(b, 7919.toBigInteger())
b = bigTen.pow(1000) + bigOne
moTest(b, BigInteger("15485863"))
b = bigTen.pow(10000) - bigOne
moTest(b, BigInteger("22801763489"))
moTest(BigInteger("1511678068"), BigInteger("7379191741"))
moTest(BigInteger("3047753288"), BigInteger("2257683301"))
}

View file

@ -1,4 +1,4 @@
my @primes := 2, grep *.is-prime, (3,5,7...*);
my @primes := 2, |grep *.is-prime, (3,5,7...*);
sub factor($a is copy) {
gather {
@ -23,9 +23,8 @@ sub mo-prime($a, $p, $e) {
for factor($t) -> $f {
@qs = flat @qs.map(-> $q { (0..$f.value).map(-> $j { $q * $f.key ** $j }) });
}
@qs.sort();
@qs.first(-> $q { expmod( $a, $q, $m ) == 1});
@qs.sort.first(-> $q { expmod( $a, $q, $m ) == 1});
}
sub mo($a, $m) {

View file

@ -0,0 +1,26 @@
func mo_prime(a, p, e) {
var m = p**e
var t = (p-1)*(p**(e-1))
var qs = [1]
for f in (t.factor_exp) {
qs.map! {|q|
0..f[1] -> map {|j| q * f[0]**j }...
}
}
qs.sort.first_by {|q| powmod(a, q, m) == 1 }
}
func mo(a, m) {
gcd(a, m) == 1 || die "#{a} and #{m} are not relatively prime"
Math.lcm(1, m.factor_exp.map {|r| mo_prime(a, r...) }...)
}
say mo(37, 1000)
say mo(54, 100001)
with (10**20 - 1) {|b|
say mo(2, b)
say mo(17, b)
}