diff --git a/DIRECTORY.md b/DIRECTORY.md index d36d53b5..17182e46 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -7,11 +7,15 @@ * [Generateparentheses.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generateparentheses.test.ts) ## Bit Manipulation + * [Add Binary](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/add_binary.ts) * [Is Power Of 2](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_2.ts) * [Is Power Of 4](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_4.ts) + * [Log Two](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/log_two.ts) * Test + * [Add Binary.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/add_binary.test.ts) * [Is Power Of 2.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_2.test.ts) * [Is Power Of 4.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_4.test.ts) + * [Log Two.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/log_two.test.ts) ## Ciphers * [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts) @@ -71,9 +75,11 @@ ## Dynamic Programming * [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts) + * [Lcs](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/lcs.ts) ## Graph * [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts) + * [Bipartite Graph](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bipartite_graph.ts) * [Dijkstra](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/dijkstra.ts) * [Floyd Warshall](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/floyd_warshall.ts) * [Johnson](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/johnson.ts) @@ -81,6 +87,7 @@ * [Prim](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/prim.ts) * Test * [Bellman Ford.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bellman_ford.test.ts) + * [Bipartite Graph.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bipartite_graph.test.ts) * [Dijkstra.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/dijkstra.test.ts) * [Floyd Warshall.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/floyd_warshall.test.ts) * [Johnson.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/johnson.test.ts) @@ -101,6 +108,7 @@ * [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts) * [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts) * [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts) + * [Gaussian Elimination](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/gaussian_elimination.ts) * [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts) * [Hamming Distance](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/hamming_distance.ts) * [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts) @@ -151,9 +159,11 @@ * [Counting Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/counting_sort.ts) * [Cycle Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/cycle_sort.ts) * [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts) + * [Heap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/heap_sort.ts) * [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts) * [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts) * [Quick Select](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_select.ts) * [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts) * [Selection Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/selection_sort.ts) * [Shell Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/shell_sort.ts) + * [Swap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/swap_sort.ts) diff --git a/maths/fibonacci.ts b/maths/fibonacci.ts index 3d4cb118..4280a083 100644 --- a/maths/fibonacci.ts +++ b/maths/fibonacci.ts @@ -10,25 +10,31 @@ * @see https://en.m.wikipedia.org/wiki/Fibonacci_number * @author MohdFaisalBidda */ -export const nthFibonacci = (number: number): number => { - if (number < 0) { - throw 'Number should be greater than 0'; +function* generateFibonacci(): Generator { + let a = 0; + let b = 1; + while (true) { + yield a; + const c = a + b; + a = b; + b = c; } +} + +export const nthFibonacci = (number: number): number => { + if (isNaN(number)) throw new Error('The input needs to be a number'); + if (!Number.isInteger(number) || number < 0) throw new Error('The input needs to be a non-negative integer'); if (number === 0) { return 0; } - - let a = 0, - b = 1; - for (let i = 1; i < number; ++i) { - const c = a + b; - - a = b; - b = c; + + const fibonacciGenerator = generateFibonacci(); + let result = 0; + for (let i = 0; i <= number; ++i) { + result = fibonacciGenerator.next().value; } - - return b; + return result; }; /** @@ -70,4 +76,4 @@ const sqrt5 = Math.sqrt(5) const phi = (1 + sqrt5) / 2 const psi = (1 - sqrt5) / 2 -export const nthFibonacciUsingFormula = (n : number) => Math.round((phi ** n - psi ** n) / sqrt5) +export const nthFibonacciUsingFormula = (n : number) => Math.round((phi ** n - psi ** n) / sqrt5) \ No newline at end of file