/
integer.js
392 lines (359 loc) · 9.92 KB
/
integer.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* <title>PointedEars' JSX: Math Library: Integer arithmetics</title>
* @requires object.js
* @requires types.js
*
* @section Copyright & Disclaimer
*
* @author
* (C) 2000-2011 Thomas Lahn <math.js@PointedEars.de>
*
* @partof PointedEars' JavaScript Extensions (JSX)
*
* JSX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JSX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with JSX. If not, see <http://www.gnu.org/licenses/>.
*/
if (typeof jsx == "undefined")
{
var jsx = {};
}
if (typeof jsx.math == "undefined")
{
jsx.math = {};
}
jsx.math.integer = {
/**
* Returns the greatest common divisor (GCD) of two integers
* <code>a</code> and <code>b</code>, implementing (the optimized form of) the
* Euclidean algorithm (also called Euclid's algorithm). The
* GCD of two integer arguments is the largest positive integer
* that both arguments are integer multiples of, i.e. by which
* either argument can be divided without remainder. Since
* integers are required, the floor of <code>a</code> and <code>b</code> will
* be used for computation.
*
* @memberOf jsx.math.integer
* @param a : number
* @param b : number
* @return type number
* The GCD of <code>a</code> and <code>b</code>;
* <code>NaN</code> if an argument is not a number.
* @see Math#floor(number)
*/
gcd: function (a, b) {
if (isNaN(a) || isNaN(b))
{
return Number.NaN;
}
a = Math.floor(a);
b = Math.floor(b);
var c;
while (b != 0)
{
c = a % b;
a = b;
b = c;
}
return Math.abs(a);
}
};
if (jsx.options.augmentBuiltins)
{
jsx.object.extend(Math, {
/**
* @memberOf Math
*/
gcd: jsx.math.integer.gcd
});
}
/**
* Returns the greatest common divisor (GCD) of two or more
* integers, implementing (the optimized form of) the Euclidean
* algorithm (also called Euclid's algorithm). The GCD of
* integer arguments is the largest positive integer that all
* arguments are integer multiples of, i.e. by which either
* argument can be divided without remainder. Since integers
* are required, the floor of the arguments will be used for
* computation.
*
* @return number
* The GCD of the arguments;
* <code>NaN</code> if one or more arguments are not a number.
* @see Math#floor(number)
*/
Math.gcdN = function(a, b) {
/* Currently supports only 2 integers */
return Math.gcd(a, b);
};
/**
* Returns the least common multiple (LCM) of two integers
* <code>a</code> and <code>b</code>. The LCM of two integer arguments is
* the smallest positive integer that is a multiple of the
* arguments. If there is no such positive integer, i.e.,
* if either argument is zero, then <code>lcm(a, b)</code>
* is defined to be zero. Since integers are required, the
* floor of <code>a</code> and <code>b</code> will be used for computation.
*
* @param a : number
* @param b : number
* @return number
* The LCM of <code>a</code> and <code>b</code>;
* <code>0</code> if an argument is <code>0</code>;
* <code>NaN</code> if an argument is not a number.
* @see Math#floor(number)
* @see Math#gcd(number, number)
*/
Math.lcm = function(a, b) {
if (isNaN(a) || isNaN(b))
{
return Number.NaN;
}
if (!a || !b)
{
return 0;
}
a = Math.floor(a);
b = Math.floor(b);
return ((a * b) / Math.gcd(a, b));
};
/**
* @author (c) 2000 Thomas Lahn <math.js@PointedEars.de>
* @param n : number
* If <code>n</code> is not an integer, <code>Math.floor(n)</code> is used.
* @return number
* The factorial of <code>n</code> (<code>n</code><code><b>!</b></code>)
* which is (for practical reasons) implemented here as follows:
* @ifdef{MathML2}
* <math>
* <apply>
* <eq/>
* <apply>
* <factorial/>
* <ci> n </ci>
* </apply>
* <piecewise>
* <piece>
* <cn>1</cn>
* <apply><lt/> <ci> n </ci> <cn> 2 </cn></apply>
* </piece>
* <piece>
* <mrow>
* <ci> n </ci>
* <mo>⁢</mo>
* <mfenced>
* <apply>
* <factorial/>
* <apply><minus/> <ci> n </ci> <cn> 1 </cn></apply>
* </apply>
* </mfenced>
* </mrow>
* <apply><gt/> <ci> n </ci> <cn> 1 </cn></apply>
* </piece>
* </piecewise>
* </apply>
* </math>
* @else
* <table>
* <tr valign="top">
* <td rowspan="2"><code>n<code><b>!</b></td>
* <td>n < 2:</td>1; (strict: 0<b>!</b> = 1<b>!</b> := 1)</td>
* </tr>
* <tr valign="top">
* <td>n > 1:</td>n * (n - 1)<b>!</b></td>
* </tr>
* <table>
* @endif
*
* Unlike common recursive implementations, the algorithm is
* implemented iterative here, saving stack space and thus
* allowing larger factorials to be computed.
*
* @throws Math#OverflowError
*/
Math.fac = function(n) {
if (n % 1 != 0)
{
n = Math.floor(n);
}
var result = 1;
for (var i = 2; i <= n; result *= i++)
{
if (result == Number.POSITIVE_INFINITY)
{
result = 0;
// [Exception] <- [MathErrorException] <- [OverflowException] }
jsx.throwThis(new Math.OverflowError("fac"));
break;
}
}
return result;
};
/**
* @author (c) 2000 Thomas Lahn <math.js@PointedEars.de>
* @param nBase : number
* @param iExponent : number
* @return number
* <var>nBase</var> to the power of <var>iExponent</var>
*/
Math.powInt = function(nBase, iExponent) {
var i = iExponent;
var result = 1.0;
while (i > 0)
{
if (i % 2)
{
result *= nBase;
}
i = Math.floor(i / 2);
nBase *= nBase;
}
return result;
};
/**
* Computes the base <code>nBase</code> powered
* by the exponent <code>nExponent</code>
* (<code>nBase</code>^<code>nExponent</code>).
* Uses the Math.exp() and Math.log() methods to
* workaround the bug in Math.pow() that positive
* roots of certain negative values, like root(-9,
* 1/3) cannot be computed (results in NaN there)
* but should be.
*
* @author
* (c) 2000-2004 Thomas Lahn <math.js@PointedEars.de>
* @param nBase : number
* @param nExponent : number
* @throws Math#InvalidArgumentError
* @return type number
* NaN if computation result lies beyond real numbers,
* the result (as double-precision floating-point number)
* otherwise.
*/
Math.power = function(nBase, nExponent) {
var result;
if (nBase != 0)
{
/* e^(Exponent * ln(|Base|)) */
result = Math.exp(nExponent * Math.log(Math.abs(nBase)));
if (nBase < 0)
{
if (nExponent % 1 == 0)
{
if (Math.floor(nExponent) % 2)
{
result = -result;
}
}
else
{
result = Number.NaN;
/* [Exception]<- [Math.MathError] <- [Math.InvalidArgumentError] */
jsx.throwThis(new Math.InvalidArgumentError(
"power(" + nBase + ", " + nExponent + ")"));
}
}
else if (nBase == 0 && nExponent == 0)
{
result = 0;
jsx.throwThis(new Math.InvalidArgumentError(
"power(" + nBase + ", " + nExponent + ")"));
}
}
return result;
};
/**
* @return type number
* The logarithm of <code>x</code> to the base <code>nBase</code>.
*
* @author (c) 2000 Thomas Lahn <math.js@PointedEars.de>
* @param x : number
* @param nBase : number
*/
Math.logN = function(x, nBase) {
return Math.log(x) / Math.log(nBase);
};
/**
* @author
* (c) 2000 Thomas Lahn <math.js@PointedEars.de>
*
* @param x : number
* @return type number
* The logarithm digitalis (<code>ld</code>) of <code>x</code>
* (the logarithm of <code>x</code> to the base 2).
*/
Math.log2 = function(x) {
return Math.logN(x, 2);
};
/**
* Computes the decimal logarithm (<code>lg</code>) of
* <code>x</code> (the log. of <code>x</code> to the base 10).
*
* @author (c) 2000 Thomas Lahn <math.js@PointedEars.de>
* @param x : number
* @return number
* The logarithm of <var>x</var> to the base 10
*/
Math.log10 = function(x) {
return Math.logN(x, 10);
};
/**
* @author
* (c) 2000 Thomas Lahn <math.js@PointedEars.de>
* @param x : number
* @return type number
* the reciprocal of <code>x</code> (<code>1/x</code>).
*/
Math.rec = function(x) {
return (1 / x);
};
/**
* Finds prime numbers within a range
*
* @param {int} upperBounds
* The upper bounds of the half-open interval [2, ceil(upperBounds)),
* that is, the smallest integer that should not be considered
* in the search.
* @return {Object}
* An object whose property names are the primes within range.
* Use {@link Object#keys()} to get an {@link Array} of
* <code><em>string</em></code> values; use
* {@link Array.prototype#map} to get an <code>Array</code>
* of {@link Number} values.
* NOTE: As of ECMAScript Edition 5.1, an <code>Array</code>
* is limited to 2^32-1 elements. Use {@link jsx.array.BigArray}
* to process this <code>Object</code> efficiently.
*/
Math.primes = function (upperBounds) {
var not_prime = Object.create(null);
var prime = Object.create(null);
var i = 2;
var upperLimit = Math.sqrt(upperBounds);
while (i < upperLimit)
{
prime[i] = true;
for (var mult = i * i; mult < upperBounds; mult += i)
{
not_prime[mult] = true;
}
while (not_prime[++i]);
}
for (; i < upperBounds; ++i)
{
if (!not_prime[i])
{
prime[i] = true;
}
delete not_prime[i];
}
return prime;
};