forked from christianp/isthisprime.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thing.js
214 lines (184 loc) · 4.76 KB
/
thing.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var seen = {};
var current_n;
var screensaver = false;
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";
}
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];
function is_prime(n) {
if(n.length<=10) {
n = parseInt(n);
if(n<2) {
return "not prime";
}
return miller(n);
}
console.log("probabilistic");
n = BigInteger.parse(n);
if(n.isEven()) {
return "composite";
}
var res;
var a;
var s = (n.toString()).replace(/\s/g,'');
var rounds = smallPrimes.length;
for (var k=0;k<rounds;k++) {
a = smallPrimes[k];
if (s == a.toString()) {
return 'prime';
}
res = miller_rabin(s,a);
if ((res.toString()).indexOf('i')>0) {
return res;
}
if (res===false) {
return 'composite';
}
}
return 'probable prime';
}
var $ = function(s){return document.querySelector(s)};
function update() {
var n = current_n;
document.body.className = '';
$('#n').innerHTML = n;
var tens = n.length-(n.length%10);
if(n.length>30) {
tens = 30;
}
document.body.classList.add('len-'+tens);
function calculate() {
var message = n in seen ? seen[n] : describe_primality(n);
seen[n] = message;
document.body.classList.remove('loading');
document.body.classList.add(message);
if(message!='not-number') {
$('#prev-n').innerHTML = BigInteger.parse(n).subtract(1).toString();
$('#next-n').innerHTML = BigInteger.parse(n).add(1).toString();
}
if(window.history) {
window.history.replaceState(null,n,n+(screensaver ? '?screensaver' : ''));
}
}
if(n.length>6) {
document.body.classList.add('loading');
setTimeout(calculate,0);
} else {
calculate();
}
}
function describe_primality(n) {
if(!n.match(/\d+/)) {
return "not-number";
}
switch(is_prime(n,50)) {
case 'composite':
case 'not prime':
return 'not-prime';
case 'prime':
return 'prime';
case 'probable prime':
return 'probably-prime';
}
}
function next() {
current_n = BigInteger(current_n).add(1).toString();
update();
}
function prev() {
current_n = BigInteger(current_n).subtract(1).toString();
update();
}
$('#prev').addEventListener('click',prev);
$('#next').addEventListener('click',next);
if(window.location.search.match(/^\?screensaver/)) {
screensaver = true;
setInterval(next,5000);
}
current_n = window.location.pathname.slice(1) || '2';
update();