forked from christianp/isthisprime.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime.js
155 lines (136 loc) · 3.39 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
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];
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 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===false) {
return 'composite';
}
}
return '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;
}