From 186dcb791c57dafee67e5b113bd09cf1562ec61b Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 15:48:33 +0530 Subject: [PATCH 1/9] added fibonacci using formula along with test cases --- Maths/FibonacciUsingFormula.js | 19 ++++++++++++++++ Maths/test/FibonacciUsingFormula.test.js | 28 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Maths/FibonacciUsingFormula.js create mode 100644 Maths/test/FibonacciUsingFormula.test.js diff --git a/Maths/FibonacciUsingFormula.js b/Maths/FibonacciUsingFormula.js new file mode 100644 index 0000000000..81af117e41 --- /dev/null +++ b/Maths/FibonacciUsingFormula.js @@ -0,0 +1,19 @@ +// https://en.wikipedia.org/wiki/Fibonacci_sequence + +/* + Nth Fibonacci Number : + Fn = (((1+sqrt(5))^n) - ((1-sqrt(5))^n))/(2^n)*sqrt(5) + + Complexities : + TC - O(1) + SC - O(1) +*/ +const FibonacciUsingFormula = (n) => { + const sqrt5 = Math.sqrt(5); + const phi = (1 + sqrt5) / 2; + const psi = (1 - sqrt5) / 2; + const result = (Math.pow(phi, n) - Math.pow(psi, n)) / sqrt5; + return Math.round(result); +} + +export { FibonacciUsingFormula }; \ No newline at end of file diff --git a/Maths/test/FibonacciUsingFormula.test.js b/Maths/test/FibonacciUsingFormula.test.js new file mode 100644 index 0000000000..2a906f304e --- /dev/null +++ b/Maths/test/FibonacciUsingFormula.test.js @@ -0,0 +1,28 @@ +import { FibonacciUsingFormula } from '../FibonacciUsingFormula' + +describe('FibonacciUsingFormula' , () => { + it('should calculate the correct Fibonacci number for n = 0', () => { + const result = FibonacciUsingFormula(0) + expect(result).toBe(0) + }) + it('should calculate the correct Fibonacci number for n = 1', () => { + const result = FibonacciUsingFormula(1) + expect(result).toBe(1) + }) + it('should calculate the correct Fibonacci number for n = 2', () => { + const result = FibonacciUsingFormula(2) + expect(result).toBe(1) + }) + it('should calculate the correct Fibonacci number for n = 5', () => { + const result = FibonacciUsingFormula(5) + expect(result).toBe(5) + }) + it('should calculate the correct Fibonacci number for n = 10', () => { + const result = FibonacciUsingFormula(10) + expect(result).toBe(55) + }) + it('should calculate the correct Fibonacci number for n = 15', () => { + const result = FibonacciUsingFormula(15) + expect(result).toBe(610) + }) +}) From 29ca3480f70b75148e4828459f48580c8af46903 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 17:41:19 +0530 Subject: [PATCH 2/9] updated the changes --- Maths/Fibonacci.js | 11 ++++++++++ Maths/FibonacciUsingFormula.js | 19 ---------------- Maths/test/Fibonacci.test.js | 14 +++++++++++- Maths/test/FibonacciUsingFormula.test.js | 28 ------------------------ 4 files changed, 24 insertions(+), 48 deletions(-) delete mode 100644 Maths/FibonacciUsingFormula.js delete mode 100644 Maths/test/FibonacciUsingFormula.test.js diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index fc8102ad2c..bc2bf79c52 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -187,9 +187,20 @@ const FibonacciMatrixExpo = (num) => { return F[0][0] * (isNeg ? (-ONE) ** (num + ONE) : ONE) } +const FibonacciUsingFormula = (n) => { + const sqrt5 = Math.sqrt(5) + const phi = (1 + sqrt5) / 2 + const psi = (1 - sqrt5) / 2 + + // x ** y states x to the power y. + const result = (phi ** n - psi ** n) / sqrt5 + return Math.round(result) +} + export { FibonacciDpWithoutRecursion } export { FibonacciIterative } export { FibonacciGenerator } export { FibonacciRecursive } export { FibonacciRecursiveDP } export { FibonacciMatrixExpo } +export { FibonacciUsingFormula } diff --git a/Maths/FibonacciUsingFormula.js b/Maths/FibonacciUsingFormula.js deleted file mode 100644 index 81af117e41..0000000000 --- a/Maths/FibonacciUsingFormula.js +++ /dev/null @@ -1,19 +0,0 @@ -// https://en.wikipedia.org/wiki/Fibonacci_sequence - -/* - Nth Fibonacci Number : - Fn = (((1+sqrt(5))^n) - ((1-sqrt(5))^n))/(2^n)*sqrt(5) - - Complexities : - TC - O(1) - SC - O(1) -*/ -const FibonacciUsingFormula = (n) => { - const sqrt5 = Math.sqrt(5); - const phi = (1 + sqrt5) / 2; - const psi = (1 - sqrt5) / 2; - const result = (Math.pow(phi, n) - Math.pow(psi, n)) / sqrt5; - return Math.round(result); -} - -export { FibonacciUsingFormula }; \ No newline at end of file diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index f3dcb98fe7..fc4fd30321 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -4,7 +4,8 @@ import { FibonacciIterative, FibonacciGenerator, FibonacciRecursive, - FibonacciMatrixExpo + FibonacciMatrixExpo, + FibonacciUsingFormula } from '../Fibonacci' describe('Fibonacci', () => { @@ -95,4 +96,15 @@ describe('Fibonacci', () => { expect(FibonacciMatrixExpo(-5n)).toBe(5n) expect(FibonacciMatrixExpo(-6n)).toBe(-8n) }) + it('should calculate the correct Fibonacci number for n = 0', () => { + expect(FibonacciUsingFormula(0)).toBe(0) + }) + + it('should calculate the correct Fibonacci number for n = 1', () => { + expect(FibonacciUsingFormula(1)).toBe(1) + }) + + it('should calculate the correct Fibonacci number for n = 15', () => { + expect(FibonacciUsingFormula(15)).toBe(610) + }) }) diff --git a/Maths/test/FibonacciUsingFormula.test.js b/Maths/test/FibonacciUsingFormula.test.js deleted file mode 100644 index 2a906f304e..0000000000 --- a/Maths/test/FibonacciUsingFormula.test.js +++ /dev/null @@ -1,28 +0,0 @@ -import { FibonacciUsingFormula } from '../FibonacciUsingFormula' - -describe('FibonacciUsingFormula' , () => { - it('should calculate the correct Fibonacci number for n = 0', () => { - const result = FibonacciUsingFormula(0) - expect(result).toBe(0) - }) - it('should calculate the correct Fibonacci number for n = 1', () => { - const result = FibonacciUsingFormula(1) - expect(result).toBe(1) - }) - it('should calculate the correct Fibonacci number for n = 2', () => { - const result = FibonacciUsingFormula(2) - expect(result).toBe(1) - }) - it('should calculate the correct Fibonacci number for n = 5', () => { - const result = FibonacciUsingFormula(5) - expect(result).toBe(5) - }) - it('should calculate the correct Fibonacci number for n = 10', () => { - const result = FibonacciUsingFormula(10) - expect(result).toBe(55) - }) - it('should calculate the correct Fibonacci number for n = 15', () => { - const result = FibonacciUsingFormula(15) - expect(result).toBe(610) - }) -}) From 8ee41dcb2b450c64db2aafe9daa7d4a7a5c40f4f Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 18:06:20 +0530 Subject: [PATCH 3/9] added jest's each in test cases --- Maths/test/Fibonacci.test.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index fc4fd30321..854a42df0f 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -96,15 +96,11 @@ describe('Fibonacci', () => { expect(FibonacciMatrixExpo(-5n)).toBe(5n) expect(FibonacciMatrixExpo(-6n)).toBe(-8n) }) - it('should calculate the correct Fibonacci number for n = 0', () => { - expect(FibonacciUsingFormula(0)).toBe(0) - }) - - it('should calculate the correct Fibonacci number for n = 1', () => { - expect(FibonacciUsingFormula(1)).toBe(1) - }) - - it('should calculate the correct Fibonacci number for n = 15', () => { - expect(FibonacciUsingFormula(15)).toBe(610) + it.each([ + [0, 0], + [1, 1], + [5, 610] + ])('should calculate the correct Fibonacci number for n = %i', (n, expected) => { + expect(FibonacciUsingFormula(n)).toBe(expected) }) }) From dad7ad2a9766864e62f6c42e55ccd9e0dd90e0c8 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 18:06:58 +0530 Subject: [PATCH 4/9] added jest's each for testing --- Maths/test/Fibonacci.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/test/Fibonacci.test.js b/Maths/test/Fibonacci.test.js index 854a42df0f..f91aef73d2 100644 --- a/Maths/test/Fibonacci.test.js +++ b/Maths/test/Fibonacci.test.js @@ -99,7 +99,7 @@ describe('Fibonacci', () => { it.each([ [0, 0], [1, 1], - [5, 610] + [15, 610] ])('should calculate the correct Fibonacci number for n = %i', (n, expected) => { expect(FibonacciUsingFormula(n)).toBe(expected) }) From 31e2b38fb4cbfd8b15a71f4ecb6fd2a5ca2ef5c8 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 18:09:17 +0530 Subject: [PATCH 5/9] returned inline value --- Maths/Fibonacci.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index bc2bf79c52..b8bd3d3336 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -193,8 +193,7 @@ const FibonacciUsingFormula = (n) => { const psi = (1 - sqrt5) / 2 // x ** y states x to the power y. - const result = (phi ** n - psi ** n) / sqrt5 - return Math.round(result) + return Math.round((phi ** n - psi ** n) / sqrt5) } export { FibonacciDpWithoutRecursion } From 363cffeac14fd826d43ba123626e77b8362b9f3b Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 18:11:01 +0530 Subject: [PATCH 6/9] removed redundant comment --- Maths/Fibonacci.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index b8bd3d3336..b4836755e4 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -192,7 +192,6 @@ const FibonacciUsingFormula = (n) => { const phi = (1 + sqrt5) / 2 const psi = (1 - sqrt5) / 2 - // x ** y states x to the power y. return Math.round((phi ** n - psi ** n) / sqrt5) } From c0a2057cfb31bdae56dade607667beb83b17ef86 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Mon, 11 Sep 2023 18:21:47 +0530 Subject: [PATCH 7/9] hoisted the variables --- Maths/Fibonacci.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index b4836755e4..9f82d1f908 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -187,11 +187,11 @@ const FibonacciMatrixExpo = (num) => { return F[0][0] * (isNeg ? (-ONE) ** (num + ONE) : ONE) } -const FibonacciUsingFormula = (n) => { - const sqrt5 = Math.sqrt(5) - const phi = (1 + sqrt5) / 2 - const psi = (1 - sqrt5) / 2 +const sqrt5 = Math.sqrt(5) +const phi = (1 + sqrt5) / 2 +const psi = (1 - sqrt5) / 2 +const FibonacciUsingFormula = (n) => { return Math.round((phi ** n - psi ** n) / sqrt5) } From cc93a8bdb1eedc806f116a71c333f23239377ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:56:06 +0200 Subject: [PATCH 8/9] Use shorthand --- Maths/Fibonacci.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 9f82d1f908..8de436706d 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -191,9 +191,7 @@ const sqrt5 = Math.sqrt(5) const phi = (1 + sqrt5) / 2 const psi = (1 - sqrt5) / 2 -const FibonacciUsingFormula = (n) => { - return Math.round((phi ** n - psi ** n) / sqrt5) -} +const FibonacciUsingFormula = n => Math.round((phi ** n - psi ** n) / sqrt5) export { FibonacciDpWithoutRecursion } export { FibonacciIterative } From 998e02efb9aac7160c897bd5b38b71ab59ac59d3 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Tue, 19 Sep 2023 17:06:28 +0530 Subject: [PATCH 9/9] considered adding resource of the formula --- Maths/Fibonacci.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Maths/Fibonacci.js b/Maths/Fibonacci.js index 8de436706d..998c753027 100644 --- a/Maths/Fibonacci.js +++ b/Maths/Fibonacci.js @@ -187,6 +187,10 @@ const FibonacciMatrixExpo = (num) => { return F[0][0] * (isNeg ? (-ONE) ** (num + ONE) : ONE) } +/* + Resource : https://math.hmc.edu/funfacts/fibonacci-number-formula/ +*/ + const sqrt5 = Math.sqrt(5) const phi = (1 + sqrt5) / 2 const psi = (1 - sqrt5) / 2