Skip to content

Commit 8e83941

Browse files
authored
feat(maths): recursive calculating of Fibonacci numbers (#107)
* feat(maths): improve fibonacci number function * feat(maths): makes recursive fibonacci an extra function * fix: improve tests * test: improves tests
1 parent 6fffa79 commit 8e83941

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

maths/fibonacci.ts

+41-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
/**
2-
* A function to get nth Fibonacci number
3-
* @param number The input integer
4-
* @return {number} Fibonacci number of `number`
2+
* A function to get nth Fibonacci number.
3+
*
4+
* Time Complexity: linear (O(n))
5+
*
6+
* @param number The index of the number in the Fibonacci sequence.
7+
* @return The Fibonacci number on the nth index in the sequence.
8+
*
59
* @example nthFibonacci(4) => 3 | nthFibonacci(6) => 8
610
* @see https://en.m.wikipedia.org/wiki/Fibonacci_number
711
* @author MohdFaisalBidda <https://github.com/MohdFaisalBidda>
812
*/
9-
1013
export const nthFibonacci = (number: number): number => {
11-
if (number < 0) throw "Number should be greater than 0";
12-
13-
if (number === 0) return 0;
14+
if (number < 0) {
15+
throw 'Number should be greater than 0';
16+
}
1417

15-
let a = 0, b = 1;
18+
if (number === 0) {
19+
return 0;
20+
}
1621

22+
let a = 0,
23+
b = 1;
1724
for (let i = 1; i < number; ++i) {
1825
const c = a + b;
1926

@@ -23,3 +30,29 @@ export const nthFibonacci = (number: number): number => {
2330

2431
return b;
2532
};
33+
34+
/**
35+
* A function to get nth Fibonacci number recursively. **Note: This recursive approach increases the time complexity**
36+
*
37+
* Time Complexity: exponential (O(ϕ^n))
38+
*
39+
* @param number The index of the number in the Fibonacci sequence.
40+
* @return The Fibonacci number on the nth index in the sequence.
41+
*
42+
* @example nthFibonacci(4) => 3 | nthFibonacci(6) => 8
43+
* @see https://en.m.wikipedia.org/wiki/Fibonacci_number
44+
* @author zFlxw <https://github.com/zFlxw>
45+
*/
46+
export const nthFibonacciRecursively = (number: number): number => {
47+
if (number === 0) {
48+
return 0;
49+
}
50+
51+
if (number <= 2) {
52+
return 1;
53+
}
54+
55+
return (
56+
nthFibonacciRecursively(number - 1) + nthFibonacciRecursively(number - 2)
57+
);
58+
};

maths/test/fibonacci.test.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import {nthFibonacci} from '../fibonacci';
1+
import { nthFibonacci, nthFibonacciRecursively } from '../fibonacci';
22

3-
describe('nthFibonacci', () => {
4-
test('should return correct value', () => {
5-
expect(nthFibonacci(0)).toBe(0);
6-
expect(nthFibonacci(1)).toBe(1);
7-
expect(nthFibonacci(5)).toBe(5);
8-
expect(nthFibonacci(4)).toBe(3);
9-
expect(nthFibonacci(0)).toBe(0);
10-
});
11-
});
3+
const test = (func: (n: number) => number) =>
4+
it.each([
5+
[0, 0],
6+
[1, 1],
7+
[2, 1],
8+
[5, 5],
9+
[10, 55],
10+
[15, 610],
11+
])('fib(%i) = %i', (n, expected) => expect(func(n)).toBe(expected));
12+
describe('Fibonacci iterative', () => test(nthFibonacci));
13+
describe('Fibonacci recursive', () => test(nthFibonacciRecursively));

0 commit comments

Comments
 (0)