Skip to content

algorithm: linear search #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Maths/SieveOfEratosthenes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @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<boolean>(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;
}
20 changes: 20 additions & 0 deletions Maths/test/SieveOfEratosthenes.test.ts
Original file line number Diff line number Diff line change
@@ -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);
},
);
});
18 changes: 18 additions & 0 deletions Search/LinearSearch.ts
Original file line number Diff line number Diff line change
@@ -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;
}
14 changes: 14 additions & 0 deletions Search/test/LinearSearch.test.ts
Original file line number Diff line number Diff line change
@@ -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)
},
);
});