-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprime.js
187 lines (166 loc) · 4.77 KB
/
prime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function modpow(a,b,n) {
if(b==0) {
return 1;
}
var t = 1;
while(b>0) {
if(b%2) {
if(t*a>Number.MAX_SAFE_INTEGER) {
t = BigInteger(t).multiply(a).remainder(n);
} else {
t = (t*a)%n;
}
b -= 1;
}
if(a*a>Number.MAX_SAFE_INTEGER) {
a = BigInteger(a).square().remainder(n);
} else {
a = (a*a)%n;
}
b = b/2;
}
return t;
}
function miller(n) {
var s = 0;
var d = n-1;
while(d%2==0) {
s += 1;
d /= 2;
}
var ln = Math.log(n);
var end = Math.min(n-1,Math.floor(2*ln*ln));
for(var a=2;a<=end;a++) {
if(modpow(a,d,n)!=1) {
var broke = false;
for(var r=0;r<=s-1;r++) {
if(modpow(a,Math.pow(2,r)*d,n)==n-1) {
broke = true;
break;
}
}
if(!broke) {
return "composite";
}
}
}
return "prime";
}
// adapted from http://www.javascripter.net/math/primes/millerrabinprimalitytest.htm
function miller_rabin(n,a) {
// miller_rabin(n,a) performs one round of the Miller-Rabin test
// n is a positive integer to be tested (can be a number or a string)
// a is the base for the Miller-Rabin test
//
// returns:
// `false` if provably composite
// `true` if probable prime
var s = (n.toString()).replace(/\s/g,'');
var len = s.length;
var f = parseFloat(s);
var lastDigit = parseInt(s[len-1],10);
var res;
var len = s.length;
var mr_base = BigInteger.parse(a.toString());
var mr_cand = BigInteger.parse(s);
var mr_temp = mr_cand.subtract(1)
var one = BigInteger(1);
var zero = BigInteger(0);
res = mrr3 (mr_base, mr_temp, mr_cand);
return res.compare(1)==0;
function mrr3(a, i, n) {
if (i.isZero()) {
return one;
}
var j = i.divide(2);
var x = mrr3(a, j, n);
if (x.isZero()) {
return zero;
}
var y = x.square().remainder(n);
if (y.compare(one)==0 && x.compare(one)!=0 && x.compare(n.subtract(1))!=0 ) {
return zero;
}
if (i.remainder(2)==1) {
y = y.multiply(a).remainder(n);
}
return y;
}
}
var smallPrimes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113];
// When n is small, testing on the following bases is sufficient to prove the primality of n
// from https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Deterministic_variants
var deterministic_bounds = [
[BigInteger.parse('2047'),[2]],
[BigInteger.parse('1373653'),[2,3]],
[BigInteger.parse('9080191'),[31,73]],
[BigInteger.parse('25326001'),[2,3,5]],
[BigInteger.parse('3215031751'),[2,3,5,7]],
[BigInteger.parse('4759123141'),[2,7,61]],
[BigInteger.parse('1122004669633'),[2,13,23,1662803]],
[BigInteger.parse('2152302898747'),[2,3,5,7,11]],
[BigInteger.parse('3474749660383'),[2,3,5,7,11,3]],
[BigInteger.parse('341550071728321'),[2,3,5,7,11,13,17]],
[BigInteger.parse('3825123056546413051'),[2,3,5,7,11,13,17,19,23]],
[BigInteger.parse('18446744073709551616'),[2,3,5,7,11,13,17,19,23,29,31,37]],
[BigInteger.parse('318665857834031151167461'),[2,3,5,7,11,13,17,19,23,29,32,37]],
[BigInteger.parse('3317044064679887385961981'),[2,3,5,7,11,13,17,19,23,29,31,37,41]]
];
function is_prime(n) {
n = n.toString();
if(n.length<=10) {
n = parseInt(n);
if(n<2) {
return "not prime";
}
return miller(n);
}
n = BigInteger.parse(n);
if(n.isEven()) {
return "composite";
}
var bases = null;
var deterministic = false;
for(var i=0;i<deterministic_bounds.length;i++) {
if(n.compare(deterministic_bounds[i][0])==-1) { // if n < the bound
bases = deterministic_bounds[i][1];
deterministic = true;
break;
}
}
if(bases===null) {
bases = smallPrimes;
}
var res;
var a;
var s = (n.toString()).replace(/\s/g,'');
var rounds = bases.length;
for (var k=0;k<rounds;k++) {
a = bases[k];
if (s == a.toString()) {
return 'prime';
}
res = miller_rabin(s,a);
if (res===false) {
return 'composite';
}
}
return deterministic ? 'prime' : 'probable prime';
}
function factorise(n) {
var factors = [];
var i = 2;
while(n>1) {
var p = 0;
while(n%i==0) {
p += 1;
n /= i;
}
if(p) {
factors.push([i,p]);
} else {
i += 1;
}
}
return factors;
}