Skip to content

Commit 86f9358

Browse files
committed
Moved sieve-of-eratosthenes to Typescript
1 parent c8daa64 commit 86f9358

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

src/seive-of-eratosthenes/README.md renamed to src/sieve-of-eratosthenes/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Seive of Eratosthenes
1+
# Sieve of Eratosthenes
22

33
## Description
44

55
The _Sieve of Eratosthenes_ is a simple algorithm for finding all of the [prime numbers](https://en.wikipedia.org/wiki/Prime_number) up to a certain limit.
66

77
## Implementation
88

9-
**_sieveOfEratosthenes(num)_** should return an array of all prime numbers up to the ```num```.
9+
`sieveOfEratosthenes(num)` should return an array of all prime numbers up to the `num`.
1010

11-
For example:
11+
Example:
1212

1313
```
1414
sieveOfEratosthenes(20) // [2, 3, 5, 7, 11, 13, 17, 19]

src/seive-of-eratosthenes/seive-of-eratosthenes.spec.js renamed to src/sieve-of-eratosthenes/index.spec.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const sieveOfEratosthenes = require('./seive-of-eratosthenes');
1+
import sieveOfEratosthenes from ".";
22

3-
describe('Seive of Eratosthenes:', () => {
3+
describe('Sieve of Eratosthenes:', () => {
44
test('should return all prime numbers up to 13', () => {
55
const primeNumbersArray = [2, 3, 5, 7, 11, 13];
66

@@ -13,10 +13,6 @@ describe('Seive of Eratosthenes:', () => {
1313
expect(sieveOfEratosthenes(27)).toEqual(primeNumbersArray);
1414
});
1515

16-
test('should return an empty array if no number passed', () => {
17-
expect(sieveOfEratosthenes()).toEqual([]);
18-
});
19-
2016
test('should return an empty array if negative number passed', () => {
2117
expect(sieveOfEratosthenes(-5)).toEqual([]);
2218
});

src/seive-of-eratosthenes/seive-of-eratosthenes.js renamed to src/sieve-of-eratosthenes/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
function sieveOfEratosthenes(num) {
2-
const primes = [];
3-
const result = [];
1+
function sieveOfEratosthenes(num: number): number[] {
2+
const primes: boolean[] = [];
3+
const result: number[] = [];
44

55
for (let i = 0; i <= num; i ++) {
66
primes[i] = true;
@@ -15,9 +15,7 @@ function sieveOfEratosthenes(num) {
1515
}
1616
}
1717

18-
const primesLength = primes.length;
19-
20-
for (let i = 0; i < primesLength; i++) {
18+
for (let i = 0; i < primes.length; i++) {
2119
if (primes[i]) {
2220
result.push(i);
2321
}
@@ -26,4 +24,4 @@ function sieveOfEratosthenes(num) {
2624
return result;
2725
}
2826

29-
module.exports = sieveOfEratosthenes;
27+
export default sieveOfEratosthenes;

0 commit comments

Comments
 (0)