@gkucmierz/utils 1.29.1 → 1.30.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 CHANGED
@@ -71,6 +71,9 @@ import {
71
71
  import {
72
72
  tonelliShanksBI
73
73
  } from './src/tonelli-shanks.mjs'
74
+ import {
75
+ naturalSearch
76
+ } from './src/natural-search.mjs'
74
77
 
75
78
  export * from './src/SetCnt.mjs';
76
79
  export * from './src/Trie.mjs';
@@ -96,7 +99,8 @@ export * from './src/pow-mod.mjs';
96
99
  export * from './src/range-array.mjs';
97
100
  export * from './src/square-root.mjs';
98
101
  export * from './src/tonelli-shanks.mjs';
102
+ export * from './src/natural-search.mjs';
99
103
 
100
104
  export default [
101
- SetCnt, Trie, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, matrixAsArray, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
105
+ SetCnt, Trie, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, matrixAsArray, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI, naturalSearch
102
106
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.29.1",
3
+ "version": "1.30.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "keywords": [
6
6
  "utils",
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Natural Search Algorithm by gkucmierz
3
+ * Mathematically finds the exponential boundary and binary searches bounds for a conditional function.
4
+ * Useful for finding bounds in sigmoidal functions without known boundaries.
5
+ * @param {Function} cond - The condition evaluator function.
6
+ * @param {Boolean} [retFirstTrue=true] - If true, returns the first match bounding condition true.
7
+ * @returns {Number} The optimized integer step boundary that satisfies the condition structure.
8
+ */
9
+ export const naturalSearch = (cond, retFirstTrue = true) => {
10
+ let min = 1;
11
+ let max = 1;
12
+ while(1) {
13
+ const stop = cond(max);
14
+ if (stop) break;
15
+ min = max;
16
+ max *= 2;
17
+ }
18
+ let mid;
19
+ while (1) {
20
+ mid = Math.floor((min + max) / 2);
21
+ const stop = cond(mid);
22
+ if (stop) {
23
+ max = mid;
24
+ } else {
25
+ min = mid;
26
+ }
27
+ const diff = max - min;
28
+ if (max - min <= 1) {
29
+ return retFirstTrue ? max : min;
30
+ }
31
+ }
32
+ };