@gkucmierz/utils 1.12.0 → 1.13.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/herons-formula.spec.mjs +41 -0
- package/src/herons-formula.mjs +13 -0
- package/src/phi.mjs +18 -2
- package/src/square-root.mjs +15 -0
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.13.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.13.0",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
heronsFormula,
|
|
4
|
+
heronsFormulaBI,
|
|
5
|
+
} from '../src/herons-formula.mjs';
|
|
6
|
+
|
|
7
|
+
describe('herons-formula', () => {
|
|
8
|
+
it('Integer', () => {
|
|
9
|
+
expect(heronsFormula(5, 12, 13)).toEqual(30);
|
|
10
|
+
expect(heronsFormula(6, 8, 10)).toEqual(24);
|
|
11
|
+
expect(heronsFormula(7, 15, 20)).toEqual(42);
|
|
12
|
+
expect(heronsFormula(17, 17, 30)).toEqual(120);
|
|
13
|
+
expect(heronsFormula(13, 37, 30)).toEqual(180);
|
|
14
|
+
expect(heronsFormula(6, 25, 29)).toEqual(60);
|
|
15
|
+
expect(heronsFormula(73, 9, 80)).toEqual(216);
|
|
16
|
+
expect(heronsFormula(12, 35, 37)).toEqual(210);
|
|
17
|
+
expect(heronsFormula(120, 109, 13)).toEqual(396);
|
|
18
|
+
expect(heronsFormula(9, 10, 17)).toEqual(36);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('Float', () => {
|
|
22
|
+
expect(heronsFormula(2, 3, 4)).toEqual(2.9047375096555625);
|
|
23
|
+
expect(heronsFormula(7, 10, 12)).toEqual(34.977671449083054);
|
|
24
|
+
expect(heronsFormula(6, 11, 12)).toEqual(32.839572165300815);
|
|
25
|
+
expect(heronsFormula(25, 25, 45)).toEqual(245.1880655741629);
|
|
26
|
+
expect(heronsFormula(10, 11, 18)).toEqual(48.59976851796724);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('BigInt', () => {
|
|
30
|
+
expect(heronsFormulaBI(5n, 12n, 13n)).toEqual(30n);
|
|
31
|
+
expect(heronsFormulaBI(6n, 8n, 10n)).toEqual(24n);
|
|
32
|
+
expect(heronsFormulaBI(7n, 15n, 20n)).toEqual(42n);
|
|
33
|
+
expect(heronsFormulaBI(17n, 17n, 30n)).toEqual(120n);
|
|
34
|
+
expect(heronsFormulaBI(13n, 37n, 30n)).toEqual(180n);
|
|
35
|
+
expect(heronsFormulaBI(6n, 25n, 29n)).toEqual(60n);
|
|
36
|
+
expect(heronsFormulaBI(73n, 9n, 80n)).toEqual(216n);
|
|
37
|
+
expect(heronsFormulaBI(12n, 35n, 37n)).toEqual(210n);
|
|
38
|
+
expect(heronsFormulaBI(120n, 109n, 13n)).toEqual(396n);
|
|
39
|
+
expect(heronsFormulaBI(9n, 10n, 17n)).toEqual(36n);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
import {
|
|
3
|
+
squareRootBI,
|
|
4
|
+
} from './square-root.mjs';
|
|
5
|
+
|
|
6
|
+
// calculate triangle area given 3 sides
|
|
7
|
+
|
|
8
|
+
const getHeronsFormula = (four, sq) => (a, b, c) => {
|
|
9
|
+
return sq((a + b + c) * (-a + b + c) * (a - b + c) * (a + b - c)) / four;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const heronsFormula = getHeronsFormula(4, n => n ** 0.5);
|
|
13
|
+
export const heronsFormulaBI = getHeronsFormula(4n, squareRootBI);
|
package/src/phi.mjs
CHANGED
|
@@ -4,6 +4,11 @@ import {
|
|
|
4
4
|
gcdBI,
|
|
5
5
|
} from './gcd.mjs';
|
|
6
6
|
|
|
7
|
+
import {
|
|
8
|
+
factors,
|
|
9
|
+
factorsBI,
|
|
10
|
+
} from './factors.mjs';
|
|
11
|
+
|
|
7
12
|
// Euler's Totient Function
|
|
8
13
|
|
|
9
14
|
const getPhi = (ONE, TWO, gcd) => {
|
|
@@ -18,5 +23,16 @@ const getPhi = (ONE, TWO, gcd) => {
|
|
|
18
23
|
};
|
|
19
24
|
}
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
// Eulers formula
|
|
27
|
+
|
|
28
|
+
const getPhiEuler = factors => {
|
|
29
|
+
return n => {
|
|
30
|
+
return [...new Set(factors(n))].reduce((res, f) => res - res / f, n);
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const phi = getPhiEuler(factors);
|
|
35
|
+
export const phiBI = getPhiEuler(factorsBI);
|
|
36
|
+
|
|
37
|
+
// export const phi = getPhi(1, 2, gcd);
|
|
38
|
+
// export const phiBI = getPhi(1n, 2n, gcdBI);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export const squareRoot = n => n ** 0.5;
|
|
4
|
+
|
|
5
|
+
// Newrton's formula
|
|
6
|
+
export const squareRootBI = n => {
|
|
7
|
+
let res = BigInt(n.toString().substr(0, n.toString().length / 2));
|
|
8
|
+
let last;
|
|
9
|
+
while (res * res - n !== 0n) {
|
|
10
|
+
last = res;
|
|
11
|
+
res = (res + n / res) / 2n;
|
|
12
|
+
if (res === last) return res;
|
|
13
|
+
}
|
|
14
|
+
return res;
|
|
15
|
+
};
|