@gkucmierz/utils 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/main.mjs +5 -1
- package/package.json +3 -2
- package/src/mobius.mjs +31 -0
package/main.mjs
CHANGED
|
@@ -83,6 +83,9 @@ import {
|
|
|
83
83
|
import {
|
|
84
84
|
memoize
|
|
85
85
|
} from './src/memoize.mjs'
|
|
86
|
+
import {
|
|
87
|
+
mobius, mobiusBI
|
|
88
|
+
} from './src/mobius.mjs'
|
|
86
89
|
import {
|
|
87
90
|
mod, modBI
|
|
88
91
|
} from './src/mod.mjs'
|
|
@@ -154,6 +157,7 @@ export * from './src/math3d.mjs';
|
|
|
154
157
|
export * from './src/matrix.mjs';
|
|
155
158
|
export * from './src/measure-performance.mjs';
|
|
156
159
|
export * from './src/memoize.mjs';
|
|
160
|
+
export * from './src/mobius.mjs';
|
|
157
161
|
export * from './src/mod.mjs';
|
|
158
162
|
export * from './src/n-choose-k.mjs';
|
|
159
163
|
export * from './src/natural-search.mjs';
|
|
@@ -170,5 +174,5 @@ export * from './src/square-root.mjs';
|
|
|
170
174
|
export * from './src/tonelli-shanks.mjs';
|
|
171
175
|
|
|
172
176
|
export default [
|
|
173
|
-
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, chunks, chunksAsyncIterator, chunksIterator, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
177
|
+
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, chunks, chunksAsyncIterator, chunksIterator, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mobius, mobiusBI, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
174
178
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Usefull functions for solving programming tasks",
|
|
6
6
|
"keywords": [
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"simulated-annealing",
|
|
53
53
|
"rand-normal",
|
|
54
54
|
"format-big-number",
|
|
55
|
-
"natural-search"
|
|
55
|
+
"natural-search",
|
|
56
|
+
"mobius"
|
|
56
57
|
],
|
|
57
58
|
"files": [
|
|
58
59
|
"main.mjs",
|
package/src/mobius.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { factors, factorsBI } from './factors.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the Mobius function μ(n) of a number.
|
|
5
|
+
* @param {number} n - A positive integer.
|
|
6
|
+
* @returns {number} The Mobius function value (1, -1, or 0).
|
|
7
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LFARiArgZwQXwQDMYowEAiAAQHMBrLAYzBAFMYAvAeizhABscFANzBgaTLlFA|▶ Try it live in Instacode}
|
|
8
|
+
*/
|
|
9
|
+
export const mobius = n => {
|
|
10
|
+
if (n < 1) return 0;
|
|
11
|
+
if (n === 1) return 1;
|
|
12
|
+
const f = factors(n);
|
|
13
|
+
const unique = new Set(f);
|
|
14
|
+
if (unique.size !== f.length) return 0;
|
|
15
|
+
return f.length % 2 === 0 ? 1 : -1;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Calculates the Mobius function μ(n) of a BigInt.
|
|
20
|
+
* @param {bigint} n - A positive BigInt.
|
|
21
|
+
* @returns {number} The Mobius function value (1, -1, or 0).
|
|
22
|
+
* @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4LFARiArgZwEICSCAvggGYxRgIBEAAgOYDWWAxmCAKYwBeA9FjggANjhoBuYMDSZchSUA|▶ Try it live in Instacode}
|
|
23
|
+
*/
|
|
24
|
+
export const mobiusBI = n => {
|
|
25
|
+
if (n < 1n) return 0;
|
|
26
|
+
if (n === 1n) return 1;
|
|
27
|
+
const f = factorsBI(n);
|
|
28
|
+
const unique = new Set(f);
|
|
29
|
+
if (unique.size !== f.length) return 0;
|
|
30
|
+
return f.length % 2 === 0 ? 1 : -1;
|
|
31
|
+
};
|