@gkucmierz/utils 1.16.1 → 1.16.2
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/square-root.spec.mjs +15 -0
- package/src/square-root.mjs +3 -1
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.16.
|
|
3
|
+
"version": "1.16.2",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.16.
|
|
9
|
+
"version": "1.16.2",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
squareRoot,
|
|
4
|
+
squareRootBI,
|
|
5
|
+
} from '../src/square-root.mjs';
|
|
6
|
+
|
|
7
|
+
describe('square-root', () => {
|
|
8
|
+
it('squareRootBI', () => {
|
|
9
|
+
expect(squareRootBI(0n)).toEqual(0n);
|
|
10
|
+
expect(squareRootBI(1n)).toEqual(1n);
|
|
11
|
+
expect(squareRootBI(2n)).toEqual(1n);
|
|
12
|
+
expect(squareRootBI(3n)).toEqual(1n);
|
|
13
|
+
expect(squareRootBI(4n)).toEqual(2n);
|
|
14
|
+
});
|
|
15
|
+
});
|
package/src/square-root.mjs
CHANGED
|
@@ -4,7 +4,9 @@ export const squareRoot = n => n ** 0.5;
|
|
|
4
4
|
|
|
5
5
|
// Newrton's formula
|
|
6
6
|
export const squareRootBI = n => {
|
|
7
|
-
|
|
7
|
+
if (n === 0n) return 0n;
|
|
8
|
+
if (n < 4n) return 1n;
|
|
9
|
+
let res = n > 10n ? BigInt(n.toString().substr(0, n.toString().length / 2)) : 1n;
|
|
8
10
|
let last;
|
|
9
11
|
while (res * res - n !== 0n) {
|
|
10
12
|
last = res;
|