-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime.js
358 lines (330 loc) · 6.46 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
////////////////////////////////////////
//
// prime.js NUMBER THEORY LIBRARY
//
// by Kardi Teknomo
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
//
////////////////////////////////////////
// return array list of primes between N1 and N2
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function ErastosthenesSieve(N1, N2)
{
N1=parseInt(N1);
N2=parseInt(N2);
var m = 0;
var prime=new Array();
if (N2<N1)
{
//swap
var temp=N1;
N1=N2;
N2=temp;
}
for(var n = N1;n<=N2; n++)
{
i=IsPrime(n);
if (i == 1)
{
prime[m] = n;
m ++;
}
}
return prime;
}
// return 1 if input N is prime, return > 1 if N is composite
// return -1 if input N is non positive integer or N<2
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function IsPrime(N)
{
if(isPosInteger(N) & N >=2 & N<MAXLIMIT)
{
Limit = Math.floor(Math.sqrt(N));
for(var i=Limit; i>=2; i--)
{
if(N % i == 0)
{
break;
}
}
return i;
}
else
{
return false;
}
}
// return text prime factorization
// return -1 if input N is non positive integer or N<2
// report repeated prime as exponent
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function PrimeFactorization(N)
{
if(isPosInteger(N) & N >=2 & N<MAXLIMIT)
{
var p = 2;
var i = 0;
var arr = new Array();
var txt = N + " = ";
while (N >= p * p)
{
if (N % p == 0)
{
arr[i]=p;
N = N / p; //divide by prime number
i++;
}
else
{
p++;
}
}
arr[i]=N;
// reporting
var e=0;
for(var j=0; j<arr.length; j++)
{
b=arr[j];
e++;
if(arr[j]!=arr[j+1])
{
if(j==arr.length-1)
{
if(e>1)
{
txt += b + "^" + e;
}
else // e==0
{
txt += b;
}
}
else
{
if(e>1)
{
txt += b + "^" + e + " * ";
}
else // e==0
{
txt += b + " * ";
}
}
e=0;
}
}
return txt;
}
else
{
return false;
}
}
// return text prime factorization
// return -1 if input N is non positive integer or N<2
// report repeated prime as repeated numbers
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function PrimeFactorization0(N)
{
if(isPosInteger(N) & N >=2 & N<MAXLIMIT)
{
p = 2;
txt = N + " = ";
while (N >= p * p)
{
if (N % p == 0)
{
txt += p + " * ";
N = N / p; //divide by prime number
}
else
{
p++;
}
}
txt += N;
return txt;
}
else
{
return false;
}
}
// return array contain list of divisors
// return -1 if input N is non positive integer
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function FindDivisors(N)
{
if(isPosInteger(N) & N<MAXLIMIT)
{
var arr= new Array();
i = 0;
Limit = Math.floor(Math.sqrt(N));
for(var k=1; k<=Limit; k++)
{
if(N % k==0)
{
arr[i]= k;
i++;
}
}
m=i--; // m = number of divisors <= sqr(N)
if(IntDiv(N,arr[m-1])==arr[m-1])
{
// if N is a perfect square
n=2*m-1; // number of divisors
for(j=0; j<m-1; j++)
{
arr[m+j]=IntDiv(N,arr[m-j-2]);
}
}
else
{
// if N is not a perfect square
n=2*m; // number of divisors
for(j=0; j<m; j++)
{
arr[m+j]=IntDiv(N,arr[m-j-1]);
}
}
return arr;
}
else
{
return false;
}
}
// general purpose function to return Boolean true if n is divisible by d
// e.g. IsDivisible(22,4) = false
// e.g. IsDivisible(20,5) = true
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function IsDivisible(nominator,denominator)
{
return (nominator % denominator==0);
}
// general purpose function to return quotient of integer division of two integers
// e.g. IntDiv(22,4) = 5 because 22/4=5.5 -> 5
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function IntDiv(nominator, denominator)
{
return Math.floor(nominator/denominator);
}
// general purpose function to return remainder of integer division of two integers
// e.g. Remainder(22,4) = 2 because 22 = 4 * 5 + 2
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function Remainder(nominator, denominator)
{
return nominator % denominator;
//return nominator-denominator*IntDiv(nominator, denominator);
}
// general purpose function to return true if a is congruent to b module m
// e.g. IsCongruent(7,2,5) = true because 5 divides (7-2)
// e.g. IsCongruent(15,20,10) = false because 10 does not divides (15-20)
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function IsCongruent(a,b,m)
{
return IsDivisible(a-b,m)
}
// general purpose function to return greatest common divisor of two integers
// e.g. gcd(12, 20) = 4
// e.g. gcd(30, 18) = 6
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function gcd(m,n)
{
if(n==0)
return m;
else
{
var k=1;
while (k!=0)
{
k = m % n;
m = n;
n = k;
}
return m;
}
}
// general purpose function to return least common multiple of two integers
// e.g. lcm(12, 20) = 60
// e.g. lcm(30, 18) = 90
// (c) 2010-2016 Kardi Teknomo all rights reserved
// http://people.revoledu.com/kardi/
function lcm(m,n)
{
g=gcd(m,n)
if(g==0)
return 0;
else
return m*n/g;
}
//general purpose function to return Integer Linear Equation ax+by=gcd(a,b)
// e.g. 60x+22y => 60x+22y=2, x=-4, y=11
function IntLinearEquation(a,b)
{
var x=1; var g=a; var v=0; var w=b;
while (w!=0)
{
var q = IntDiv(g, w);
var t = Remainder(g, w);
var s = x - q*v;
x = v; g = w;
v = s; w = t;
}
if(b==0)
{
g=gcd(a,b);
y=1;
if(a==0)
x=1;
}
else
y=(g-a*x)/b;
return Array(g,x,y);
}
function LinearDiophantineEquation(a,b,c)
{
var g=gcd(a,b);
alert(g);
if(IsDivisible(c,g)==false)
{
alert("no solution");
return false; //no solution
}
else
{
alert("has solution");
arr=IntLinearEquation(a,c);
alert(arr);
g=arr[0];
x=arr[1];
y=arr[2];
x0=b*x/g;
alert(x0);
return x0;
}
}
// general purpose function to see if a suspected numeric input
// is a positive integer
// taken from JavaScript™ Bible 5th Edition by Danny Goodman and Michael Morrison
function isPosInteger(inputVal) {
inputStr = inputVal.toString();
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.charAt(i);
if (oneChar < "0" || oneChar > "9") {
return false;
}
}
return true;
}