UNPKG

9.4 kBJavaScriptView Raw
1function is(x) {
2 return typeof x === "bigint";
3}
4function compare(x, y) {
5 return Number(x - y);
6}
7function abs(x) {
8 return x < 0n ? -x : x;
9}
10function sign(x) {
11 return x < 0n ? -1n : (x > 0n ? 1n : 0n);
12}
13function floorDiv(x, y) {
14 if (y < 0n) {
15 x = -x;
16 y = -y;
17 }
18 return x >= 0n ? x / y : (x - y + 1n) / y;
19}
20function ceilDiv(x, y) {
21 if (y < 0n) {
22 x = -x;
23 y = -y;
24 }
25 return x >= 0n ? (x + y - 1n) / y : x / y;
26}
27function roundDiv(x, y) {
28 if (y < 0n) {
29 x = -x;
30 y = -y;
31 }
32 return x >= 0n ? (x + y / 2n) / y : (x - y / 2n) / y;
33}
34function rem(x, y) {
35 return x % y;
36}
37function mod(x, y) {
38 return x - y * floorDiv(x, y);
39}
40function modp(x, y) {
41 return x - abs(y) * floorDiv(x, abs(y));
42}
43function constrain(x, minv, maxv) {
44 return min(max(x, minv), maxv);
45}
46function remap(x, r, R, t, T) {
47 return t + (x - r) * (T - t) / (R - r);
48}
49function lerp(x, y, t) {
50 return x + BigInt(Math.floor(t * Number(y - x)));
51}
52function isPow2(x) {
53 return /^10*$/.test(x.toString(2));
54}
55function isPow10(x) {
56 return /^10*$/.test(x.toString());
57}
58function prevPow2(x) {
59 if (x <= 1n)
60 return 0n;
61 var n = x.toString(2).length;
62 return BigInt("0b1" + "0".repeat(n - 1));
63}
64function prevPow10(x) {
65 if (x <= 1n)
66 return 0n;
67 var n = x.toString(10).length;
68 return BigInt("1" + "0".repeat(n - 1));
69}
70function nextPow2(x) {
71 if (x <= 0n)
72 return 1n;
73 var n = (x - 1n).toString(2).length;
74 return BigInt("0b1" + "0".repeat(n));
75}
76function nextPow10(x) {
77 if (x <= 0n)
78 return 1n;
79 var n = (x - 1n).toString(10).length;
80 return BigInt("1" + "0".repeat(n));
81}
82function log2(x) {
83 var n = x.toString(2).length - 1;
84 return x <= 0n ? 0n : BigInt(n);
85}
86function log10(x) {
87 var n = x.toString().length - 1;
88 return x <= 0n ? 0n : BigInt(n);
89}
90function sqrt(x) {
91 if (x === 0n)
92 return 0n;
93 return x > 0n ? sqrtPos(x) : null;
94}
95function sqrtPos(x) {
96 var a = 1n << (log2(x) / 2n + 1n);
97 var b = 1n + a;
98 while (a < b) {
99 b = a;
100 a = (b + x / b) / 2n;
101 }
102 return b;
103}
104function cbrt(x) {
105 return root(x, 3n);
106}
107function root(x, n = 1n) {
108 if (x === 0n)
109 return 0n;
110 else if (x > 0n)
111 return rootPos(x, n);
112 return n % 2n !== 0n ? -rootPos(-x, n) : null;
113}
114function rootPos(x, n) {
115 var a = 1n << (log2(x) / n + 1n);
116 var b = 1n + a, m = n - 1n;
117 if (a === 2n)
118 return 1n;
119 while (a < b) {
120 b = a;
121 a = (m * b + x / b ** m) / n;
122 }
123 return b;
124}
125function properDivisors(x) {
126 var x = abs(x), a = [];
127 for (var i = 1n; i < x; i++)
128 if (x % i === 0n)
129 a.push(i);
130 return a;
131}
132function aliquotSum(x) {
133 var x = abs(x), a = 0n;
134 for (var i = 1n; i < x; i++)
135 if (x % i === 0n)
136 a += i;
137 return a;
138}
139function minPrimeFactor(x) {
140 var x = abs(x);
141 if (x <= 1n)
142 return 0n;
143 if (x <= 3n)
144 return x;
145 if (x % 2n === 0n)
146 return 2n;
147 if (x % 3n === 0n)
148 return 3n;
149 for (var i = 6n, I = sqrt(x) + 1n; i <= I; i += 6n) {
150 if (x % (i - 1n) === 0n)
151 return i - 1n;
152 if (x % (i + 1n) === 0n)
153 return i + 1n;
154 }
155 return x;
156}
157function maxPrimeFactor(x) {
158 var x = abs(x), a = 0n;
159 if (x <= 1n)
160 return 0n;
161 if (x <= 3n)
162 return x;
163 for (; x % 2n === 0n; a = 2n)
164 x /= 2n;
165 for (; x % 3n === 0n; a = 3n)
166 x /= 3n;
167 for (var i = 6n, I = sqrt(x) + 1n; x > 1n && i <= I; i += 6n) {
168 for (; x % (i - 1n) == 0n; a = i - 1n)
169 x /= i - 1n;
170 for (; x % (i + 1n) == 0n; a = i + 1n)
171 x /= i + 1n;
172 }
173 if (x <= 1n)
174 return a;
175 return x;
176}
177function primeFactors(x) {
178 var x = abs(x), a = [];
179 if (x <= 1n)
180 return [];
181 if (x <= 3n)
182 return [x];
183 x = pushPrimeFactorTo$(a, x, 2n);
184 x = pushPrimeFactorTo$(a, x, 3n);
185 for (var i = 6n, I = sqrt(x) + 1n; x > 1n && i <= I; i += 6n) {
186 x = pushPrimeFactorTo$(a, x, i - 1n);
187 x = pushPrimeFactorTo$(a, x, i + 1n);
188 }
189 if (x > 1n)
190 a.push(x);
191 return a;
192}
193function pushPrimeFactorTo$(a, x, f) {
194 if (x % f !== 0n)
195 return x;
196 do {
197 x /= f;
198 } while (x % f === 0n);
199 a.push(f);
200 return x;
201}
202function primeExponentials(x) {
203 var x = abs(x), a = [];
204 if (x <= 1n)
205 return [];
206 if (x <= 3n)
207 return [[x, 1n]];
208 x = pushPrimeExponentialTo$(a, x, 2n);
209 x = pushPrimeExponentialTo$(a, x, 3n);
210 for (var i = 6n, I = sqrt(x) + 1n; x > 1n && i <= I; i += 6n) {
211 x = pushPrimeExponentialTo$(a, x, i - 1n);
212 x = pushPrimeExponentialTo$(a, x, i + 1n);
213 }
214 if (x > 1n)
215 a.push([x, 1n]);
216 return a;
217}
218function pushPrimeExponentialTo$(a, x, f) {
219 if (x % f !== 0n)
220 return x;
221 var e = 0n;
222 do {
223 x /= f;
224 ++e;
225 } while (x % f === 0n);
226 a.push([f, e]);
227 return x;
228}
229function isPrime(x) {
230 return x !== 0n && minPrimeFactor(x) === abs(x);
231}
232function gcd(...xs) {
233 var a = xs[0] || 1n;
234 for (var i = 1, I = xs.length; i < I; i++)
235 a = gcdPair(a, xs[i]);
236 return a;
237}
238function gcdPair(x, y) {
239 while (y !== 0n) {
240 var t = y;
241 y = x % y;
242 x = t;
243 }
244 return x;
245}
246function lcm(...xs) {
247 var a = xs[0] || 1n;
248 for (var i = 1, I = xs.length; i < I; i++)
249 a = a * xs[i] / gcdPair(a, xs[i]);
250 return a;
251}
252function factorial(n, k = 0n) {
253 if (n < 0n)
254 return 0n;
255 for (var i = k + 1n, a = 1n; i <= n; i++)
256 a *= i;
257 return a;
258}
259function binomial(n, k) {
260 if (k < 0n || k > abs(n))
261 return 0n;
262 if (n < 0n)
263 return ((-1n) ** k) * binomial(-n, k);
264 k = k > n - k ? n - k : k;
265 for (var a = 1n, b = 1n, i = 1n; i <= k; i++, n--) {
266 a *= n;
267 b *= i;
268 }
269 return a / b;
270}
271function multinomial(...ks) {
272 var n = sum(...ks), a = 1n, b = 1n;
273 for (var i = 0, j = 0n, I = ks.length; i < I;) {
274 if (j <= 0n)
275 j = ks[i++];
276 else {
277 a *= n--;
278 b *= j--;
279 }
280 }
281 return a / b;
282}
283function hypot(...xs) {
284 var a = 0n;
285 for (var x of xs)
286 a += x * x;
287 return sqrt(a);
288}
289function sum(...xs) {
290 var a = 0n;
291 for (var x of xs)
292 a += x;
293 return a;
294}
295function product(...xs) {
296 var a = 1n;
297 for (var x of xs)
298 a *= x;
299 return a;
300}
301function median(...xs) {
302 if (xs.length === 0)
303 return 0n;
304 xs.sort(compare);
305 var i = xs.length >> 1;
306 if ((xs.length & 1) === 1)
307 return xs[i];
308 return (xs[i - 1] + xs[i]) / 2n;
309}
310function modes(...xs) {
311 xs.sort(compare);
312 var r = maxRepeat(xs);
313 return getRepeats(xs, r);
314}
315function maxRepeat(xs) {
316 var count = Math.min(xs.length, 1), max = count;
317 for (var i = 1, I = xs.length; i < I; i++) {
318 if (xs[i - 1] === xs[i])
319 count++;
320 else {
321 max = Math.max(max, count);
322 count = 1;
323 }
324 }
325 return Math.max(max, count);
326}
327function getRepeats(xs, r) {
328 var a = [];
329 r--;
330 for (var i = 0, I = xs.length - r; i < I; i++)
331 if (xs[i] === xs[i + r])
332 a.push(xs[i += r]);
333 return a;
334}
335function min(...xs) {
336 if (xs.length === 0)
337 return 0n;
338 var a = xs[0];
339 for (var x of xs)
340 a = x < a ? x : a;
341 return a;
342}
343function max(...xs) {
344 if (xs.length === 0)
345 return 0n;
346 var a = xs[0];
347 for (var x of xs)
348 a = x > a ? x : a;
349 return a;
350}
351function range(...xs) {
352 if (xs.length === 0)
353 return [0n, 0n];
354 var a = xs[0], b = a;
355 for (var x of xs) {
356 a = x < a ? x : a;
357 b = x > b ? x : b;
358 }
359 return [a, b];
360}
361function variance(...xs) {
362 if (xs.length === 0)
363 return 0n;
364 var m = arithmeticMean(...xs), a = 0n;
365 for (var x of xs)
366 a += (x - m) ** 2n;
367 return a / BigInt(xs.length);
368}
369function arithmeticMean(...xs) {
370 var n = BigInt(xs.length);
371 return sum(...xs) / n;
372}
373function geometricMean(...xs) {
374 var n = BigInt(xs.length);
375 return root(product(...xs), n);
376}
377function harmonicMean(...xs) {
378 var n = BigInt(xs.length);
379 var p = product(...xs), q = 0n;
380 for (var x of xs)
381 q += p / x;
382 return n * p / q;
383}
384function quadriaticMean(...xs) {
385 var n = BigInt(xs.length);
386 var a = 0n;
387 for (var x of xs)
388 a += x * x;
389 return sqrt(a / n);
390}
391function cubicMean(...xs) {
392 var n = BigInt(xs.length);
393 var a = 0n;
394 for (var x of xs)
395 a += x ** 3n;
396 return cbrt(a / n);
397}
398
399export { abs, properDivisors as aliquotParts, aliquotSum, arithmeticMean, binomial, cbrt, ceilDiv, constrain as clamp, compare, constrain, cubicMean, factorial, floorDiv, gcd, geometricMean, maxPrimeFactor as greatestPrimeFactor, harmonicMean, gcd as hcf, hypot, is, isPow10, isPow2, isPrime, lcm, minPrimeFactor as leastPrimeFactor, lerp, log10, log2, remap as map, max, maxPrimeFactor, arithmeticMean as mean, median, min, minPrimeFactor, mod, modes, modp, multinomial, nextPow10, nextPow2, prevPow10, prevPow2, primeExponentials, primeFactors, product, properDivisors, quadriaticMean, range, rem, remap, root, quadriaticMean as rootMeanSquare, roundDiv, sign, sqrt, sum, variance };