@gkucmierz/utils 1.6.0 → 1.7.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/package-lock.json +2 -2
- package/package.json +1 -1
- package/spec/pow-mod.spec.mjs +21 -0
- package/src/pow-mod.mjs +17 -24
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.7.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
powMod,
|
|
4
|
+
powModBI,
|
|
5
|
+
} from '../src/pow-mod.mjs';
|
|
6
|
+
|
|
7
|
+
describe('pow-mod', () => {
|
|
8
|
+
it('powMod', () => {
|
|
9
|
+
expect(powMod(2, 1)).toEqual(2);
|
|
10
|
+
expect(powMod(2, 1, 10)).toEqual(2);
|
|
11
|
+
expect(powMod(2, 1, 2)).toEqual(0);
|
|
12
|
+
expect(powMod(2, 0, 2)).toEqual(1);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('powModBI', () => {
|
|
16
|
+
expect(powModBI(2n, 1n)).toEqual(2n);
|
|
17
|
+
expect(powModBI(2n, 1n, 10n)).toEqual(2n);
|
|
18
|
+
expect(powModBI(2n, 1n, 2n)).toEqual(0n);
|
|
19
|
+
expect(powModBI(2n, 0n, 2n)).toEqual(1n);
|
|
20
|
+
});
|
|
21
|
+
});
|
package/src/pow-mod.mjs
CHANGED
|
@@ -1,30 +1,23 @@
|
|
|
1
1
|
|
|
2
2
|
// implementation of power function with modulus like native python pow
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
const getPowMod = (ZERO, ONE, TWO, floor) => {
|
|
5
|
+
return (base, exponent, modulus = null) => {
|
|
6
|
+
if (modulus === null) return base ** exponent;
|
|
7
|
+
if (modulus === ONE) return ZERO;
|
|
8
|
+
let result = ONE;
|
|
9
|
+
base = base % modulus;
|
|
10
|
+
while (exponent > ZERO) {
|
|
11
|
+
if (exponent % TWO === ONE) {
|
|
12
|
+
result = (result * base) % modulus;
|
|
13
|
+
}
|
|
14
|
+
exponent = floor(exponent / TWO);
|
|
15
|
+
base = (base * base) % modulus;
|
|
11
16
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
return result;
|
|
17
|
+
return result;
|
|
18
|
+
};
|
|
16
19
|
};
|
|
17
20
|
|
|
18
|
-
export const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
base = base % modulus;
|
|
22
|
-
while (exponent > 0n) {
|
|
23
|
-
if (exponent % 2n === 1n) {
|
|
24
|
-
result = (result * base) % modulus;
|
|
25
|
-
}
|
|
26
|
-
exponent = exponent / 2n;
|
|
27
|
-
base = (base * base) % modulus;
|
|
28
|
-
}
|
|
29
|
-
return result;
|
|
30
|
-
};
|
|
21
|
+
export const powMod = getPowMod(0, 1, 2, n => Math.floor(n));
|
|
22
|
+
|
|
23
|
+
export const powModBI = getPowMod(0n, 1n, 2n, n => n);
|