@gkucmierz/utils 2.0.6 → 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 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'
@@ -128,6 +131,7 @@ export * from './src/heap.mjs';
128
131
  export * from './src/herons-formula.mjs';
129
132
  export * from './src/lcm.mjs';
130
133
  export * from './src/list-node.mjs';
134
+ export * from './src/lucas-lehmer.mjs';
131
135
  export * from './src/math3d.mjs';
132
136
  export * from './src/matrix.mjs';
133
137
  export * from './src/memoize.mjs';
@@ -146,5 +150,5 @@ export * from './src/square-root.mjs';
146
150
  export * from './src/tonelli-shanks.mjs';
147
151
 
148
152
  export default [
149
- 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, randNormal, 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
150
154
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "2.0.6",
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
+ };