@gkucmierz/utils 2.0.5 → 2.0.7
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/README.md +1 -0
- package/main.mjs +9 -1
- package/package.json +5 -1
- package/src/lucas-lehmer.mjs +18 -0
- package/src/rand-normal.mjs +5 -0
package/README.md
CHANGED
|
@@ -41,6 +41,7 @@ This library provides a wide range of mathematical functions and data structures
|
|
|
41
41
|
- `mod`, `powMod`: Modular arithmetic with Python-like behavior for negative numbers.
|
|
42
42
|
- `egcd`: Extended Euclidean Algorithm.
|
|
43
43
|
- `tonelliShanksBI`: Modular square root algorithm.
|
|
44
|
+
- `lucasLehmerBI`: Lucas-Lehmer primality test for Mersenne primes.
|
|
44
45
|
|
|
45
46
|
- **Sequences & Formulas**:
|
|
46
47
|
- `gpn`: Generalized Pentagonal Numbers.
|
package/main.mjs
CHANGED
|
@@ -59,6 +59,9 @@ import {
|
|
|
59
59
|
import {
|
|
60
60
|
ListNode
|
|
61
61
|
} from './src/list-node.mjs'
|
|
62
|
+
import {
|
|
63
|
+
lucasLehmerBI
|
|
64
|
+
} from './src/lucas-lehmer.mjs'
|
|
62
65
|
import {
|
|
63
66
|
axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball
|
|
64
67
|
} from './src/math3d.mjs'
|
|
@@ -92,6 +95,9 @@ import {
|
|
|
92
95
|
import {
|
|
93
96
|
powMod, powModBI
|
|
94
97
|
} from './src/pow-mod.mjs'
|
|
98
|
+
import {
|
|
99
|
+
randNormal
|
|
100
|
+
} from './src/rand-normal.mjs'
|
|
95
101
|
import {
|
|
96
102
|
array2range, range2array
|
|
97
103
|
} from './src/range-array.mjs'
|
|
@@ -125,6 +131,7 @@ export * from './src/heap.mjs';
|
|
|
125
131
|
export * from './src/herons-formula.mjs';
|
|
126
132
|
export * from './src/lcm.mjs';
|
|
127
133
|
export * from './src/list-node.mjs';
|
|
134
|
+
export * from './src/lucas-lehmer.mjs';
|
|
128
135
|
export * from './src/math3d.mjs';
|
|
129
136
|
export * from './src/matrix.mjs';
|
|
130
137
|
export * from './src/memoize.mjs';
|
|
@@ -136,11 +143,12 @@ export * from './src/particle-swarm.mjs';
|
|
|
136
143
|
export * from './src/permutations.mjs';
|
|
137
144
|
export * from './src/phi.mjs';
|
|
138
145
|
export * from './src/pow-mod.mjs';
|
|
146
|
+
export * from './src/rand-normal.mjs';
|
|
139
147
|
export * from './src/range-array.mjs';
|
|
140
148
|
export * from './src/simulated-annealing.mjs';
|
|
141
149
|
export * from './src/square-root.mjs';
|
|
142
150
|
export * from './src/tonelli-shanks.mjs';
|
|
143
151
|
|
|
144
152
|
export default [
|
|
145
|
-
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, array2range, range2array, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
153
|
+
SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
|
|
146
154
|
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Usefull functions for solving programming tasks",
|
|
6
6
|
"keywords": [
|
|
@@ -19,8 +19,10 @@
|
|
|
19
19
|
"iterator",
|
|
20
20
|
"javascript",
|
|
21
21
|
"lcm",
|
|
22
|
+
"lucas-lehmer",
|
|
22
23
|
"math",
|
|
23
24
|
"memoize",
|
|
25
|
+
"mersenne",
|
|
24
26
|
"mod",
|
|
25
27
|
"n-choose-k",
|
|
26
28
|
"node",
|
|
@@ -29,6 +31,8 @@
|
|
|
29
31
|
"permutations",
|
|
30
32
|
"phi",
|
|
31
33
|
"pow-mod",
|
|
34
|
+
"primality-test",
|
|
35
|
+
"prime",
|
|
32
36
|
"range-array",
|
|
33
37
|
"square-root",
|
|
34
38
|
"tonelli-shanks",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates whether a given Mersenne number (2^p - 1) is prime using the Lucas-Lehmer primality test.
|
|
3
|
+
*
|
|
4
|
+
* @param {number|bigint} exp - The exponent p of the Mersenne number M_p.
|
|
5
|
+
* @param {boolean} [verbose=false] - If true, logs the calculation progress.
|
|
6
|
+
* @returns {boolean} True if the Mersenne number is prime, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export const lucasLehmerBI = (exp, verbose = false) => {
|
|
9
|
+
const steps = exp - 2;
|
|
10
|
+
const prime = 2n ** BigInt(exp) - 1n;
|
|
11
|
+
verbose && console.log('prime: ', prime);
|
|
12
|
+
let s = 4n;
|
|
13
|
+
for (let i = 0; i < steps; ++i) {
|
|
14
|
+
s = (s ** 2n - 2n) % prime;
|
|
15
|
+
verbose && console.log(i, s);
|
|
16
|
+
}
|
|
17
|
+
return s === 0n;
|
|
18
|
+
};
|