From bf7442404fa95bd18d04df7742e542b89ecf8842 Mon Sep 17 00:00:00 2001 From: zFlxw Date: Sun, 5 Mar 2023 13:29:50 +0100 Subject: [PATCH 1/4] feat(maths): improve fibonacci number function --- maths/fibonacci.ts | 31 ++++++++++++++----------------- maths/test/fibonacci.test.ts | 15 +++++++++++---- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/maths/fibonacci.ts b/maths/fibonacci.ts index b924c91a..8a5079db 100644 --- a/maths/fibonacci.ts +++ b/maths/fibonacci.ts @@ -1,25 +1,22 @@ /** - * A function to get nth Fibonacci number - * @param number The input integer - * @return {number} Fibonacci number of `number` + * A function to get `n`th Fibonacci number. + * + * @param n The index of the number in the Fibonacci sequence. + * @return The Fibonacci number on the `n`th index in the sequence. + * * @example nthFibonacci(4) => 3 | nthFibonacci(6) => 8 * @see https://en.m.wikipedia.org/wiki/Fibonacci_number * @author MohdFaisalBidda + * @author zFlxw */ - -export const nthFibonacci = (number: number): number => { - if (number < 0) throw "Number should be greater than 0"; - - 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; +export const nthFibonacci = (n: number): number => { + if (n <= 0) { + return 0; } - return b; + if (n <= 2) { + return 1; + } + + return nthFibonacci(n - 1) + nthFibonacci(n - 2); }; diff --git a/maths/test/fibonacci.test.ts b/maths/test/fibonacci.test.ts index 578d7b79..75ab3536 100644 --- a/maths/test/fibonacci.test.ts +++ b/maths/test/fibonacci.test.ts @@ -1,11 +1,18 @@ import {nthFibonacci} 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); }); }); \ No newline at end of file From e9fdb9828e435ff18e289828cf87828e0f20a251 Mon Sep 17 00:00:00 2001 From: zFlxw Date: Sun, 5 Mar 2023 14:34:42 +0100 Subject: [PATCH 2/4] feat(maths): makes recursive fibonacci an extra function --- maths/fibonacci.ts | 56 +++++++++++++++++++++++++++++------- maths/test/fibonacci.test.ts | 19 +++++++++++- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/maths/fibonacci.ts b/maths/fibonacci.ts index 8a5079db..33013d68 100644 --- a/maths/fibonacci.ts +++ b/maths/fibonacci.ts @@ -1,22 +1,58 @@ /** - * A function to get `n`th Fibonacci number. - * - * @param n The index of the number in the Fibonacci sequence. - * @return The Fibonacci number on the `n`th index in the sequence. - * + * 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 + */ +export const nthFibonacci = (number: number): number => { + if (number < 0) { + throw 'Number should be greater than 0'; + } + + 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; + } + + 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 */ -export const nthFibonacci = (n: number): number => { - if (n <= 0) { +export const nthFibonacciRecursively = (number: number): number => { + if (number === 0) { return 0; } - if (n <= 2) { + if (number <= 2) { return 1; } - - return nthFibonacci(n - 1) + nthFibonacci(n - 2); + + return ( + nthFibonacciRecursively(number - 1) + nthFibonacciRecursively(number - 2) + ); }; diff --git a/maths/test/fibonacci.test.ts b/maths/test/fibonacci.test.ts index 75ab3536..6a3df817 100644 --- a/maths/test/fibonacci.test.ts +++ b/maths/test/fibonacci.test.ts @@ -1,4 +1,4 @@ -import {nthFibonacci} from '../fibonacci'; +import {nthFibonacci, nthFibonacciRecursively} from '../fibonacci'; describe('nthFibonacci', () => { it('should return the value of 0', () => { @@ -15,4 +15,21 @@ describe('nthFibonacci', () => { 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); + }); }); \ No newline at end of file From 1422a5b61a2aa9502d370e971f47e851bfc0ac3f Mon Sep 17 00:00:00 2001 From: zFlxw Date: Sun, 5 Mar 2023 15:52:46 +0100 Subject: [PATCH 3/4] fix: improve tests --- maths/test/fibonacci.test.ts | 57 ++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/maths/test/fibonacci.test.ts b/maths/test/fibonacci.test.ts index 6a3df817..00d6bb20 100644 --- a/maths/test/fibonacci.test.ts +++ b/maths/test/fibonacci.test.ts @@ -1,35 +1,36 @@ -import {nthFibonacci, nthFibonacciRecursively} from '../fibonacci'; +import { nthFibonacci, nthFibonacciRecursively } from '../fibonacci'; describe('nthFibonacci', () => { - it('should return the value of 0', () => { - expect(nthFibonacci(0)).toBe(0); + it.each([ true, false ])('should return the value of 0', (recursive: boolean) => { + if (recursive) { + expect(nthFibonacciRecursively(0)).toBe(0); + } else { + expect(nthFibonacci(0)).toBe(0); + } }); - it('should return the value of 1', () => { - expect(nthFibonacci(1)).toBe(1); - expect(nthFibonacci(2)).toBe(1); + it.each([true, false])('should return the value of 1', (recursive: boolean) => { + if (recursive) { + expect(nthFibonacciRecursively(1)).toBe(1); + expect(nthFibonacciRecursively(2)).toBe(1); + } else { + 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(10)).toBe(55); - expect(nthFibonacci(15)).toBe(610); - }); + it.each([true, false])( + 'should return the value from the fibonacci sequence', + (recursive: boolean) => { + if (recursive) { + expect(nthFibonacciRecursively(5)).toBe(5); + expect(nthFibonacciRecursively(10)).toBe(55); + expect(nthFibonacciRecursively(15)).toBe(610); + } else { + expect(nthFibonacci(5)).toBe(5); + 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); - }); -}); \ No newline at end of file From 60d60e40e8afa971fb05ea9f46ee18170dee00de Mon Sep 17 00:00:00 2001 From: zFlxw Date: Sun, 5 Mar 2023 18:09:44 +0100 Subject: [PATCH 4/4] test: improves tests --- maths/test/fibonacci.test.ts | 45 +++++++++--------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/maths/test/fibonacci.test.ts b/maths/test/fibonacci.test.ts index 00d6bb20..e0a8d14b 100644 --- a/maths/test/fibonacci.test.ts +++ b/maths/test/fibonacci.test.ts @@ -1,36 +1,13 @@ import { nthFibonacci, nthFibonacciRecursively } from '../fibonacci'; -describe('nthFibonacci', () => { - it.each([ true, false ])('should return the value of 0', (recursive: boolean) => { - if (recursive) { - expect(nthFibonacciRecursively(0)).toBe(0); - } else { - expect(nthFibonacci(0)).toBe(0); - } - }); - - it.each([true, false])('should return the value of 1', (recursive: boolean) => { - if (recursive) { - expect(nthFibonacciRecursively(1)).toBe(1); - expect(nthFibonacciRecursively(2)).toBe(1); - } else { - expect(nthFibonacci(1)).toBe(1); - expect(nthFibonacci(2)).toBe(1); - } - }); - - it.each([true, false])( - 'should return the value from the fibonacci sequence', - (recursive: boolean) => { - if (recursive) { - expect(nthFibonacciRecursively(5)).toBe(5); - expect(nthFibonacciRecursively(10)).toBe(55); - expect(nthFibonacciRecursively(15)).toBe(610); - } else { - expect(nthFibonacci(5)).toBe(5); - expect(nthFibonacci(10)).toBe(55); - expect(nthFibonacci(15)).toBe(610); - } - }, - ); -}); +const test = (func: (n: number) => number) => + it.each([ + [0, 0], + [1, 1], + [2, 1], + [5, 5], + [10, 55], + [15, 610], + ])('fib(%i) = %i', (n, expected) => expect(func(n)).toBe(expected)); +describe('Fibonacci iterative', () => test(nthFibonacci)); +describe('Fibonacci recursive', () => test(nthFibonacciRecursively));