Skip to content

feat(maths): recursive calculating of Fibonacci numbers #107

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
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 41 additions & 8 deletions maths/fibonacci.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
/**
* A function to get nth Fibonacci number
* @param number The input integer
* @return {number} Fibonacci number of `number`
* A function to get nth Fibonacci number.
*
* Time Complexity: linear (O(n))
*
* @param number The index of the number in the Fibonacci sequence.
* @return The Fibonacci number on the nth index in the sequence.
*
* @example nthFibonacci(4) => 3 | nthFibonacci(6) => 8
* @see https://en.m.wikipedia.org/wiki/Fibonacci_number
* @author MohdFaisalBidda <https://github.com/MohdFaisalBidda>
*/

export const nthFibonacci = (number: number): number => {
if (number < 0) throw "Number should be greater than 0";

if (number === 0) return 0;
if (number < 0) {
throw 'Number should be greater than 0';
}

let a = 0, b = 1;
if (number === 0) {
return 0;
}

let a = 0,
b = 1;
for (let i = 1; i < number; ++i) {
const c = a + b;

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

return b;
};

/**
* A function to get nth Fibonacci number recursively. **Note: This recursive approach increases the time complexity**
*
* Time Complexity: exponential (O(ϕ^n))
*
* @param number The index of the number in the Fibonacci sequence.
* @return The Fibonacci number on the nth index in the sequence.
*
* @example nthFibonacci(4) => 3 | nthFibonacci(6) => 8
* @see https://en.m.wikipedia.org/wiki/Fibonacci_number
* @author zFlxw <https://github.com/zFlxw>
*/
export const nthFibonacciRecursively = (number: number): number => {
if (number === 0) {
return 0;
}

if (number <= 2) {
return 1;
}

return (
nthFibonacciRecursively(number - 1) + nthFibonacciRecursively(number - 2)
);
};
34 changes: 29 additions & 5 deletions maths/test/fibonacci.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import {nthFibonacci} from '../fibonacci';
import {nthFibonacci, nthFibonacciRecursively} from '../fibonacci';

describe('nthFibonacci', () => {
test('should return correct value', () => {
expect(nthFibonacci(0)).toBe(0);
it('should return the value of 0', () => {
expect(nthFibonacci(0)).toBe(0);
});

it('should return the value of 1', () => {
expect(nthFibonacci(1)).toBe(1);
expect(nthFibonacci(2)).toBe(1);
});

it('should return the value from the fibonacci sequence', () => {
expect(nthFibonacci(5)).toBe(5);
expect(nthFibonacci(4)).toBe(3);
expect(nthFibonacci(0)).toBe(0);
expect(nthFibonacci(10)).toBe(55);
expect(nthFibonacci(15)).toBe(610);
});
});

describe('nthFibonacciRecursively', () => {
it('should return the value of 0', () => {
expect(nthFibonacciRecursively(0)).toBe(0);
});

it('should return the value of 1', () => {
expect(nthFibonacciRecursively(1)).toBe(1);
expect(nthFibonacciRecursively(2)).toBe(1);
});

it('should return the value from the fibonacci sequence', () => {
expect(nthFibonacciRecursively(5)).toBe(5);
expect(nthFibonacciRecursively(10)).toBe(55);
expect(nthFibonacciRecursively(15)).toBe(610);
});
});