MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / canonicalInteger

Function canonicalInteger

src/compute-engine/numerics/numeric.ts:117–164  ·  view source on GitHub ↗
(
  n: number,
  exponent: number
)

Source from the content-addressed store, hash-verified

115 return ri;
116
117 return r;
118}
119
120/* @todo Consider https://cp-algorithms.com/algebra/factorization.html */
121
122/** Return `[factor, root]` such that
123 * pow(n, 1/exponent) = factor * pow(root, 1/exponent)
124 *
125 * canonicalInteger(75, 2) -> [5, 3] = 5^2 * 3
126 *
127 */
128export function canonicalInteger(
129 n: number,
130 exponent: number
131): readonly [factor: number, root: number] {
132 if (n >= Number.MAX_SAFE_INTEGER) return [1, n];
133 if (n === 0) return [0, 0];
134 if (n === 1) return [1, 1];
135 // @todo: handle negative n
136 console.assert(Number.isInteger(n) && n > 0 && n < Number.MAX_SAFE_INTEGER);
137 if (exponent === 2) {
138 const result = (
139 [
140 [0, 0],
141 [1, 1],
142 [1, 2],
143 [1, 3],
144 [2, 1],
145 [1, 5],
146 [1, 6],
147 [1, 7],
148 [2, 2], // √8 = 2√2
149 [3, 1],
150 [1, 10],
151 [1, 11],
152 [2, 3],
153 [1, 13],
154 [1, 14],
155 [1, 15],
156 [4, 1],
157 [1, 17],
158 [3, 2],
159 [1, 19],
160 [2, 5], // √20 = 2√5
161 ] as const
162 )[n];
163 if (result) return result;
164 }
165 const factors = primeFactors(n);
166 let f = BigInt(1);
167 let r = BigInt(1);

Callers 4

lnMethod · 0.90
normalizeMethod · 0.90
_normalizeImMethod · 0.90
mulComponentsFunction · 0.90

Calls 4

primeFactorsFunction · 0.90
assertMethod · 0.80
keysMethod · 0.65
isIntegerMethod · 0.45

Tested by

no test coverage detected