From fab4f2c3e2bbcbb54dd8c255faa53d9f44d0bd86 Mon Sep 17 00:00:00 2001 From: Vinicius Cardoso Novaes Date: Wed, 26 Oct 2022 22:25:01 -0300 Subject: [PATCH 1/4] Sieve Of Eatosthenes implementation --- Maths/SieveOfEratosthenes.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Maths/SieveOfEratosthenes.ts diff --git a/Maths/SieveOfEratosthenes.ts b/Maths/SieveOfEratosthenes.ts new file mode 100644 index 00000000..df6c16b2 --- /dev/null +++ b/Maths/SieveOfEratosthenes.ts @@ -0,0 +1,24 @@ +/** + * @function SieveOfEratosthenes + * @description Find the prime numbers between 2 and n + * @param {number} n - numbers set the limit that the algorithm needs to look to find the primes + * @return {number[]} - List of prime numbers + * @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\ + * @example SieveOfErastosthenes(5) = [2,3,5] + * @example SieveOfErastosthenes(10) = [2,3,5,7] + */ + +export function SieveOfEratosthenes(n: number): number[] { + const numbers = new Array(n + 1) + .fill(true) + const primeNumbers: number[] = []; + for (let i = 2; i <= n; i++) { + if (numbers[i]) { + primeNumbers.push(i); + for (let j = i + i; j <= n; j += i) { + numbers[i] = false; + } + } + } + return primeNumbers; +} From 499488aab71e44b99edc88f97b4f08f1f55a0bc9 Mon Sep 17 00:00:00 2001 From: Vinicius Cardoso Novaes Date: Wed, 26 Oct 2022 22:48:40 -0300 Subject: [PATCH 2/4] Test addition --- Maths/SieveOfEratosthenes.ts | 5 ++++- Maths/test/SieveOfEratosthenes.test.ts | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Maths/test/SieveOfEratosthenes.test.ts diff --git a/Maths/SieveOfEratosthenes.ts b/Maths/SieveOfEratosthenes.ts index df6c16b2..29f29707 100644 --- a/Maths/SieveOfEratosthenes.ts +++ b/Maths/SieveOfEratosthenes.ts @@ -9,6 +9,9 @@ */ export function SieveOfEratosthenes(n: number): number[] { + if (n < 0 || !Number.isInteger(n)) { + throw new Error("Only natural numbers are supported"); + } const numbers = new Array(n + 1) .fill(true) const primeNumbers: number[] = []; @@ -16,7 +19,7 @@ export function SieveOfEratosthenes(n: number): number[] { if (numbers[i]) { primeNumbers.push(i); for (let j = i + i; j <= n; j += i) { - numbers[i] = false; + numbers[j] = false; } } } diff --git a/Maths/test/SieveOfEratosthenes.test.ts b/Maths/test/SieveOfEratosthenes.test.ts new file mode 100644 index 00000000..f835240f --- /dev/null +++ b/Maths/test/SieveOfEratosthenes.test.ts @@ -0,0 +1,20 @@ +import { SieveOfEratosthenes } from "../SieveOfEratosthenes"; + + +describe("Sieve of Eratosthenes", () => { + test.each([-2, 0.1, -0.01, 2.2])( + "should throw a error for non natural number", + (n) => { + expect(() => SieveOfEratosthenes(n)).toThrow( + "Only natural numbers are supported" + ); + }, + ); + + test.each([[5, [2, 3, 5]], [11, [2, 3, 5, 7, 11]], [30, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]]])( + "of %i should be %o", + (num, expected) => { + expect(SieveOfEratosthenes(num)).toStrictEqual(expected); + }, + ); +}); From 03bfa694dea0207530310b5cfcb0c6bc6aea2264 Mon Sep 17 00:00:00 2001 From: Vinicius Cardoso Novaes Date: Wed, 26 Oct 2022 23:22:05 -0300 Subject: [PATCH 3/4] Adding linear search --- Search/LinearSearch.ts | 18 ++++++++++++++++++ Search/test/LinearSearch.test.ts | 14 ++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Search/LinearSearch.ts create mode 100644 Search/test/LinearSearch.test.ts diff --git a/Search/LinearSearch.ts b/Search/LinearSearch.ts new file mode 100644 index 00000000..50a9459f --- /dev/null +++ b/Search/LinearSearch.ts @@ -0,0 +1,18 @@ +/** + * @function linearSearch + * @description linear search is the simplest search possible in a array + * it has a linear cost, if the value is present in the array, then the index of the first occurence will be returned + * if it's not present, the return it will be -1 + * @param {number[]} array - list of numbers + * @param {number} target - target number to search for + * @return {number} - index of the target number in the list, or -1 if not found + * @see https://en.wikipedia.org/wiki/Linear_search\ + * @example linearSearch([1,2,3,5], 3) => 2 + * @example linearSearch([1,5,6], 2) => -1 + */ +export const linearSearch = (array: any[], target: any): number => { + for (let i = 0; i < array.length; i++) { + if (array[i] == target) return i; + } + return -1; +} \ No newline at end of file diff --git a/Search/test/LinearSearch.test.ts b/Search/test/LinearSearch.test.ts new file mode 100644 index 00000000..71021022 --- /dev/null +++ b/Search/test/LinearSearch.test.ts @@ -0,0 +1,14 @@ +import { linearSearch } from "../LinearSearch"; + +describe("Linear search", () => { + test.each([ + [['o', 'b', 'c'], 'c', 2], + [[1, 2, 3, 4, 5], 4, 3], + [['s', 't', 'r', 'i', 'n', 'g'], 'a', -1] + ])( + "of %o , searching for %o, expected %i", + (array: any[], target: any, index: number) => { + expect(linearSearch(array, target)).toStrictEqual(index) + }, + ); +}); From 69275a2d5780433269ce3c52e7a823aa1f3e998b Mon Sep 17 00:00:00 2001 From: Vinicius Novaes Date: Thu, 27 Oct 2022 10:53:41 -0300 Subject: [PATCH 4/4] Making suggested changes --- Maths/SieveOfEratosthenes.ts | 27 -------------------------- Maths/test/SieveOfEratosthenes.test.ts | 20 ------------------- Search/LinearSearch.ts | 2 +- 3 files changed, 1 insertion(+), 48 deletions(-) delete mode 100644 Maths/SieveOfEratosthenes.ts delete mode 100644 Maths/test/SieveOfEratosthenes.test.ts diff --git a/Maths/SieveOfEratosthenes.ts b/Maths/SieveOfEratosthenes.ts deleted file mode 100644 index 29f29707..00000000 --- a/Maths/SieveOfEratosthenes.ts +++ /dev/null @@ -1,27 +0,0 @@ -/** - * @function SieveOfEratosthenes - * @description Find the prime numbers between 2 and n - * @param {number} n - numbers set the limit that the algorithm needs to look to find the primes - * @return {number[]} - List of prime numbers - * @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes\ - * @example SieveOfErastosthenes(5) = [2,3,5] - * @example SieveOfErastosthenes(10) = [2,3,5,7] - */ - -export function SieveOfEratosthenes(n: number): number[] { - if (n < 0 || !Number.isInteger(n)) { - throw new Error("Only natural numbers are supported"); - } - const numbers = new Array(n + 1) - .fill(true) - const primeNumbers: number[] = []; - for (let i = 2; i <= n; i++) { - if (numbers[i]) { - primeNumbers.push(i); - for (let j = i + i; j <= n; j += i) { - numbers[j] = false; - } - } - } - return primeNumbers; -} diff --git a/Maths/test/SieveOfEratosthenes.test.ts b/Maths/test/SieveOfEratosthenes.test.ts deleted file mode 100644 index f835240f..00000000 --- a/Maths/test/SieveOfEratosthenes.test.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { SieveOfEratosthenes } from "../SieveOfEratosthenes"; - - -describe("Sieve of Eratosthenes", () => { - test.each([-2, 0.1, -0.01, 2.2])( - "should throw a error for non natural number", - (n) => { - expect(() => SieveOfEratosthenes(n)).toThrow( - "Only natural numbers are supported" - ); - }, - ); - - test.each([[5, [2, 3, 5]], [11, [2, 3, 5, 7, 11]], [30, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]]])( - "of %i should be %o", - (num, expected) => { - expect(SieveOfEratosthenes(num)).toStrictEqual(expected); - }, - ); -}); diff --git a/Search/LinearSearch.ts b/Search/LinearSearch.ts index 50a9459f..ae03288a 100644 --- a/Search/LinearSearch.ts +++ b/Search/LinearSearch.ts @@ -12,7 +12,7 @@ */ export const linearSearch = (array: any[], target: any): number => { for (let i = 0; i < array.length; i++) { - if (array[i] == target) return i; + if (array[i] === target) return i; } return -1; } \ No newline at end of file