Skip to content

Commit deaa0af

Browse files
feat: Fibonacci Series (#205)
* Feat: Fibonacci Series * Update DIRECTORY.md * feat: generator for fibonacci function * Update DIRECTORY.md * Update DIRECTORY.md --------- Co-authored-by: autoprettier <[email protected]>
1 parent 2716e21 commit deaa0af

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

DIRECTORY.md

+10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
* [Generateparentheses.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generateparentheses.test.ts)
88

99
## Bit Manipulation
10+
* [Add Binary](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/add_binary.ts)
1011
* [Is Power Of 2](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_2.ts)
1112
* [Is Power Of 4](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/is_power_of_4.ts)
13+
* [Log Two](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/log_two.ts)
1214
* Test
15+
* [Add Binary.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/add_binary.test.ts)
1316
* [Is Power Of 2.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_2.test.ts)
1417
* [Is Power Of 4.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/is_power_of_4.test.ts)
18+
* [Log Two.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/test/log_two.test.ts)
1519

1620
## Ciphers
1721
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)
@@ -70,16 +74,19 @@
7074

7175
## Dynamic Programming
7276
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
77+
* [Lcs](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/lcs.ts)
7378

7479
## Graph
7580
* [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts)
81+
* [Bipartite Graph](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bipartite_graph.ts)
7682
* [Dijkstra](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/dijkstra.ts)
7783
* [Floyd Warshall](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/floyd_warshall.ts)
7884
* [Johnson](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/johnson.ts)
7985
* [Kruskal](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/kruskal.ts)
8086
* [Prim](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/prim.ts)
8187
* Test
8288
* [Bellman Ford.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bellman_ford.test.ts)
89+
* [Bipartite Graph.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bipartite_graph.test.ts)
8390
* [Dijkstra.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/dijkstra.test.ts)
8491
* [Floyd Warshall.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/floyd_warshall.test.ts)
8592
* [Johnson.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/johnson.test.ts)
@@ -100,6 +107,7 @@
100107
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
101108
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
102109
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts)
110+
* [Gaussian Elimination](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/gaussian_elimination.ts)
103111
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts)
104112
* [Hamming Distance](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/hamming_distance.ts)
105113
* [Is Divisible](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_divisible.ts)
@@ -150,9 +158,11 @@
150158
* [Counting Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/counting_sort.ts)
151159
* [Cycle Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/cycle_sort.ts)
152160
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts)
161+
* [Heap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/heap_sort.ts)
153162
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts)
154163
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts)
155164
* [Quick Select](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_select.ts)
156165
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts)
157166
* [Selection Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/selection_sort.ts)
158167
* [Shell Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/shell_sort.ts)
168+
* [Swap Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/swap_sort.ts)

maths/fibonacci.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,31 @@
1010
* @see https://en.m.wikipedia.org/wiki/Fibonacci_number
1111
* @author MohdFaisalBidda <https://github.com/MohdFaisalBidda>
1212
*/
13-
export const nthFibonacci = (number: number): number => {
14-
if (number < 0) {
15-
throw 'Number should be greater than 0';
13+
function* generateFibonacci(): Generator<number> {
14+
let a = 0;
15+
let b = 1;
16+
while (true) {
17+
yield a;
18+
const c = a + b;
19+
a = b;
20+
b = c;
1621
}
22+
}
23+
24+
export const nthFibonacci = (number: number): number => {
25+
if (isNaN(number)) throw new Error('The input needs to be a number');
26+
if (!Number.isInteger(number) || number < 0) throw new Error('The input needs to be a non-negative integer');
1727

1828
if (number === 0) {
1929
return 0;
2030
}
21-
22-
let a = 0,
23-
b = 1;
24-
for (let i = 1; i < number; ++i) {
25-
const c = a + b;
26-
27-
a = b;
28-
b = c;
31+
32+
const fibonacciGenerator = generateFibonacci();
33+
let result = 0;
34+
for (let i = 0; i <= number; ++i) {
35+
result = fibonacciGenerator.next().value;
2936
}
30-
31-
return b;
37+
return result;
3238
};
3339

3440
/**
@@ -70,4 +76,4 @@ const sqrt5 = Math.sqrt(5)
7076
const phi = (1 + sqrt5) / 2
7177
const psi = (1 - sqrt5) / 2
7278

73-
export const nthFibonacciUsingFormula = (n : number) => Math.round((phi ** n - psi ** n) / sqrt5)
79+
export const nthFibonacciUsingFormula = (n : number) => Math.round((phi ** n - psi ** n) / sqrt5)

0 commit comments

Comments
 (0)