@gabrielrufino/cerebrum 1.4.0 → 1.5.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.
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SieveOfErastosthenes = void 0;
|
|
4
|
+
class SieveOfErastosthenes {
|
|
5
|
+
constructor(limit) {
|
|
6
|
+
this.limit = limit;
|
|
7
|
+
}
|
|
8
|
+
execute() {
|
|
9
|
+
if (this.limit < 2)
|
|
10
|
+
return [];
|
|
11
|
+
const isPrime = Array(this.limit + 1).fill(true);
|
|
12
|
+
isPrime[0] = false;
|
|
13
|
+
isPrime[1] = false;
|
|
14
|
+
for (let i = 2; i * i <= this.limit; i++) {
|
|
15
|
+
if (isPrime[i]) {
|
|
16
|
+
for (let j = i * i; j <= this.limit; j += i) {
|
|
17
|
+
isPrime[j] = false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return isPrime
|
|
22
|
+
.map((prime, index) => prime ? index : -1)
|
|
23
|
+
.filter(num => num !== -1);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.SieveOfErastosthenes = SieveOfErastosthenes;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gabrielrufino/cerebrum",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Algorithms made in TypeScript",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"@commitlint/config-conventional": "^19.6.0",
|
|
20
20
|
"@faker-js/faker": "^8.4.1",
|
|
21
21
|
"@gabrielrufino/eslint-config": "^1.6.0",
|
|
22
|
-
"@vitest/coverage-v8": "^2.1.
|
|
22
|
+
"@vitest/coverage-v8": "^2.1.8",
|
|
23
23
|
"eslint": "^8.57.1",
|
|
24
24
|
"husky": "^9.1.7",
|
|
25
25
|
"typescript": "^5.7.2",
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { SieveOfErastosthenes } from './SieveOfErastosthenes';
|
|
4
|
+
|
|
5
|
+
describe('SieveOfErastosthenes', () => {
|
|
6
|
+
it('should return an empty array if the limit is less than 2', () => {
|
|
7
|
+
const primes = new SieveOfErastosthenes(1)
|
|
8
|
+
.execute();
|
|
9
|
+
|
|
10
|
+
expect(primes).toEqual([]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should return correct primes for small limits', () => {
|
|
14
|
+
const primes = new SieveOfErastosthenes(10)
|
|
15
|
+
.execute();
|
|
16
|
+
|
|
17
|
+
expect(primes).toEqual([2, 3, 5, 7]);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should return correct primes for a larger limit', () => {
|
|
21
|
+
const primes = new SieveOfErastosthenes(20)
|
|
22
|
+
.execute();
|
|
23
|
+
|
|
24
|
+
expect(primes).toEqual([2, 3, 5, 7, 11, 13, 17, 19]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should handle the edge case of limit = 2', () => {
|
|
28
|
+
const primes = new SieveOfErastosthenes(2)
|
|
29
|
+
.execute();
|
|
30
|
+
expect(primes).toEqual([2]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should handle the edge case of limit = 0', () => {
|
|
34
|
+
const primes = new SieveOfErastosthenes(0)
|
|
35
|
+
.execute();
|
|
36
|
+
expect(primes).toEqual([]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should handle the edge case of limit = 100', () => {
|
|
40
|
+
const primes = new SieveOfErastosthenes(100)
|
|
41
|
+
.execute();
|
|
42
|
+
|
|
43
|
+
expect(primes).toEqual([
|
|
44
|
+
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
|
|
45
|
+
53, 59, 61, 67, 71, 73, 79, 83, 89, 97
|
|
46
|
+
]);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class SieveOfErastosthenes {
|
|
2
|
+
constructor (
|
|
3
|
+
private readonly limit: number
|
|
4
|
+
) {}
|
|
5
|
+
|
|
6
|
+
public execute(): Array<number> {
|
|
7
|
+
if (this.limit < 2) return []
|
|
8
|
+
|
|
9
|
+
const isPrime = Array(this.limit + 1).fill(true)
|
|
10
|
+
isPrime[0] = false
|
|
11
|
+
isPrime[1] = false
|
|
12
|
+
|
|
13
|
+
for (let i = 2; i * i <= this.limit; i++) {
|
|
14
|
+
if (isPrime[i]) {
|
|
15
|
+
for (let j = i * i; j <= this.limit; j += i) {
|
|
16
|
+
isPrime[j] = false
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return isPrime
|
|
22
|
+
.map((prime, index) => prime ? index : -1)
|
|
23
|
+
.filter(num => num !== -1)
|
|
24
|
+
}
|
|
25
|
+
}
|