@thi.ng/math 5.1.2 → 5.2.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2021-11-21T17:09:28Z
3
+ - **Last updated**: 2021-12-13T10:26:00Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [5.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/math@5.2.0) (2021-12-13)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add prime number fns ([f301256](https://github.com/thi-ng/umbrella/commit/f301256))
17
+ - add nearestPrime()
18
+ - add primesUntil()
19
+ - add tests
20
+ - update pkg
21
+
12
22
  ## [5.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/math@5.1.0) (2021-11-17)
13
23
 
14
24
  #### 🚀 Features
package/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export * from "./libc.js";
11
11
  export * from "./min-error.js";
12
12
  export * from "./mix.js";
13
13
  export * from "./prec.js";
14
+ export * from "./prime.js";
14
15
  export * from "./ratio.js";
15
16
  export * from "./safe-div.js";
16
17
  export * from "./solve.js";
package/index.js CHANGED
@@ -11,6 +11,7 @@ export * from "./libc.js";
11
11
  export * from "./min-error.js";
12
12
  export * from "./mix.js";
13
13
  export * from "./prec.js";
14
+ export * from "./prime.js";
14
15
  export * from "./ratio.js";
15
16
  export * from "./safe-div.js";
16
17
  export * from "./solve.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/math",
3
- "version": "5.1.2",
3
+ "version": "5.2.0",
4
4
  "description": "Assorted common math functions & utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,15 +34,15 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.2"
37
+ "@thi.ng/api": "^8.3.3"
38
38
  },
39
39
  "devDependencies": {
40
- "@microsoft/api-extractor": "^7.18.19",
41
- "@thi.ng/testament": "^0.2.2",
40
+ "@microsoft/api-extractor": "^7.19.2",
41
+ "@thi.ng/testament": "^0.2.3",
42
42
  "rimraf": "^3.0.2",
43
43
  "tools": "^0.0.1",
44
- "typedoc": "^0.22.9",
45
- "typescript": "^4.5.2"
44
+ "typedoc": "^0.22.10",
45
+ "typescript": "^4.5.3"
46
46
  },
47
47
  "keywords": [
48
48
  "animation",
@@ -52,6 +52,7 @@
52
52
  "interpolation",
53
53
  "interval",
54
54
  "math",
55
+ "prime",
55
56
  "quadratic",
56
57
  "smooth",
57
58
  "solver",
@@ -112,6 +113,9 @@
112
113
  "./prec": {
113
114
  "import": "./prec.js"
114
115
  },
116
+ "./prime": {
117
+ "import": "./prime.js"
118
+ },
115
119
  "./ratio": {
116
120
  "import": "./ratio.js"
117
121
  },
@@ -128,5 +132,5 @@
128
132
  "thi.ng": {
129
133
  "year": 2013
130
134
  },
131
- "gitHead": "e8a7c2a40191b391cef182c2978e5a6c85987a87\n"
135
+ "gitHead": "2db9dd34c0c2c60cbfde3dad0bca352b20292f5c\n"
132
136
  }
package/prime.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Returns iterator of all prime numbers ≤ given `x` using Sieve of
3
+ * Eratosthenes.
4
+ *
5
+ * @remarks
6
+ * Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
7
+ *
8
+ * @param x
9
+ */
10
+ export declare function primesUntil(x: number): Generator<number, void, unknown>;
11
+ /**
12
+ * Returns largest prime number ≤ given `x` using Sieve of Eratosthenes. Returns
13
+ * -1 if x < 1.
14
+ *
15
+ * @remarks
16
+ * Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
17
+ *
18
+ * @param x
19
+ */
20
+ export declare const nearestPrime: (x: number) => number;
21
+ //# sourceMappingURL=prime.d.ts.map
package/prime.js ADDED
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Returns iterator of all prime numbers ≤ given `x` using Sieve of
3
+ * Eratosthenes.
4
+ *
5
+ * @remarks
6
+ * Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
7
+ *
8
+ * @param x
9
+ */
10
+ export function* primesUntil(x) {
11
+ if (x < 1)
12
+ return;
13
+ yield 1;
14
+ const sieve = [];
15
+ const max = Math.sqrt(x) | 0;
16
+ for (let i = 2; i <= x; i++) {
17
+ if (!sieve[i]) {
18
+ yield i;
19
+ __updateSieve(sieve, i, x, max);
20
+ }
21
+ }
22
+ }
23
+ /**
24
+ * Returns largest prime number ≤ given `x` using Sieve of Eratosthenes. Returns
25
+ * -1 if x < 1.
26
+ *
27
+ * @remarks
28
+ * Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
29
+ *
30
+ * @param x
31
+ */
32
+ export const nearestPrime = (x) => {
33
+ if (x < 1)
34
+ return -1;
35
+ let prime = 1;
36
+ const sieve = [];
37
+ const max = Math.sqrt(x) | 0;
38
+ for (let i = 2; i <= x; i++) {
39
+ if (!sieve[i]) {
40
+ prime = i;
41
+ __updateSieve(sieve, i, x, max);
42
+ }
43
+ }
44
+ return prime;
45
+ };
46
+ /**
47
+ * @internal
48
+ */
49
+ const __updateSieve = (sieve, i, x, max) => {
50
+ if (i <= max) {
51
+ for (let j = i * i; j <= x; j += i)
52
+ sieve[j] = true;
53
+ }
54
+ };