From 4eb570341d539936651f7c70c798ac8ff3b9f73e Mon Sep 17 00:00:00 2001 From: nikikalwar Date: Tue, 15 Dec 2020 07:12:46 +0530 Subject: [PATCH 1/8] added the condition to handle negative integers --- .../math/fast-powering/__test__/fastPowering.test.js | 3 +++ src/algorithms/math/fast-powering/fastPowering.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js index 0a5da756..ae8683df 100644 --- a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js +++ b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js @@ -19,5 +19,8 @@ describe('fastPowering', () => { expect(fastPowering(16, 16)).toBe(18446744073709552000); expect(fastPowering(7, 21)).toBe(558545864083284000); expect(fastPowering(100, 9)).toBe(1000000000000000000); + expect(fastPowering(5, -5)).toBe(0.0003200000000000002); + expect(fastPowering(-5, 5)).toBe(-3125); + expect(fastPowering(-5, -5)).toBe(-0.0003200000000000002); }); }); diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index 4f4a6b35..66036799 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -13,6 +13,13 @@ export default function fastPowering(base, power) { // Anything that is raised to the power of zero is 1. return 1; } + + if(power<0){ + power=power*-1; + base=1/base; + // console.log(base); + fastPowering(base,power); + } if (power % 2 === 0) { // If the power is even... From 3b1fcd83338f10a8765edf4dd79337b42a5160ae Mon Sep 17 00:00:00 2001 From: nikikalwar Date: Thu, 17 Dec 2020 06:39:36 +0530 Subject: [PATCH 2/8] fixed:error --- src/algorithms/math/fast-powering/fastPowering.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index 66036799..4f4a6b35 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -13,13 +13,6 @@ export default function fastPowering(base, power) { // Anything that is raised to the power of zero is 1. return 1; } - - if(power<0){ - power=power*-1; - base=1/base; - // console.log(base); - fastPowering(base,power); - } if (power % 2 === 0) { // If the power is even... From 506aefbc782b7c87b8bd6ca88340d976b6e9bf35 Mon Sep 17 00:00:00 2001 From: nikikalwar Date: Thu, 17 Dec 2020 06:50:04 +0530 Subject: [PATCH 3/8] fixed the boundary condition --- src/algorithms/math/fast-powering/fastPowering.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index 4f4a6b35..2c957340 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -13,6 +13,13 @@ export default function fastPowering(base, power) { // Anything that is raised to the power of zero is 1. return 1; } + + if (power < 0 ) { + const powerNext = power * -1; + const baseNext = 1 / base; + // console.log("sssss") + return fastPowering(baseNext, powerNext); + } if (power % 2 === 0) { // If the power is even... From 5ed34c7d05867f899f472331b75cbafc5ae5a8d6 Mon Sep 17 00:00:00 2001 From: nikikalwar Date: Thu, 17 Dec 2020 06:55:48 +0530 Subject: [PATCH 4/8] fixed the boundary condition and linted --- src/algorithms/math/fast-powering/fastPowering.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index 2c957340..b025af77 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -13,12 +13,12 @@ export default function fastPowering(base, power) { // Anything that is raised to the power of zero is 1. return 1; } - - if (power < 0 ) { + + if (power < 0) { const powerNext = power * -1; const baseNext = 1 / base; - // console.log("sssss") - return fastPowering(baseNext, powerNext); + // console.log("sssss") + return fastPowering(baseNext, powerNext); } if (power % 2 === 0) { From 43b806f89cdb27ab107368eda7a340d2d8675d90 Mon Sep 17 00:00:00 2001 From: nikikalwar <67374278+nikikalwar@users.noreply.github.com> Date: Fri, 18 Jun 2021 14:52:15 +0530 Subject: [PATCH 5/8] Update fastPowering.test.js --- .../math/fast-powering/__test__/fastPowering.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js index ae8683df..0a1df3e0 100644 --- a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js +++ b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js @@ -19,8 +19,8 @@ describe('fastPowering', () => { expect(fastPowering(16, 16)).toBe(18446744073709552000); expect(fastPowering(7, 21)).toBe(558545864083284000); expect(fastPowering(100, 9)).toBe(1000000000000000000); - expect(fastPowering(5, -5)).toBe(0.0003200000000000002); + expect(fastPowering(5, -5)).toBe(-0.0003200000000000002); expect(fastPowering(-5, 5)).toBe(-3125); - expect(fastPowering(-5, -5)).toBe(-0.0003200000000000002); + expect(fastPowering(-5, -5)).toBe(0.0003200000000000002); }); }); From 604e9cdd4e5be0f32f3647d6674a8d1e356b6c8a Mon Sep 17 00:00:00 2001 From: "Niki.kalwar" Date: Wed, 23 Jun 2021 22:18:33 +0530 Subject: [PATCH 6/8] edited the comments --- src/algorithms/math/fast-powering/fastPowering.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index b025af77..0b8ab0dd 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -17,7 +17,7 @@ export default function fastPowering(base, power) { if (power < 0) { const powerNext = power * -1; const baseNext = 1 / base; - // console.log("sssss") + //negative powers handled here return fastPowering(baseNext, powerNext); } From 2c221989b15d7015c93b71b9ac616fba28eb86ab Mon Sep 17 00:00:00 2001 From: nikikalwar <67374278+nikikalwar@users.noreply.github.com> Date: Wed, 23 Jun 2021 22:49:33 +0530 Subject: [PATCH 7/8] Update fastPowering.js --- src/algorithms/math/fast-powering/fastPowering.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/algorithms/math/fast-powering/fastPowering.js b/src/algorithms/math/fast-powering/fastPowering.js index 0b8ab0dd..a7d56c33 100644 --- a/src/algorithms/math/fast-powering/fastPowering.js +++ b/src/algorithms/math/fast-powering/fastPowering.js @@ -17,7 +17,6 @@ export default function fastPowering(base, power) { if (power < 0) { const powerNext = power * -1; const baseNext = 1 / base; - //negative powers handled here return fastPowering(baseNext, powerNext); } From 310343e0e1092520464529809cd9d8baa53608dc Mon Sep 17 00:00:00 2001 From: nikikalwar <67374278+nikikalwar@users.noreply.github.com> Date: Wed, 23 Jun 2021 22:50:12 +0530 Subject: [PATCH 8/8] Update fastPowering.test.js --- .../math/fast-powering/__test__/fastPowering.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js index 0a1df3e0..ae8683df 100644 --- a/src/algorithms/math/fast-powering/__test__/fastPowering.test.js +++ b/src/algorithms/math/fast-powering/__test__/fastPowering.test.js @@ -19,8 +19,8 @@ describe('fastPowering', () => { expect(fastPowering(16, 16)).toBe(18446744073709552000); expect(fastPowering(7, 21)).toBe(558545864083284000); expect(fastPowering(100, 9)).toBe(1000000000000000000); - expect(fastPowering(5, -5)).toBe(-0.0003200000000000002); + expect(fastPowering(5, -5)).toBe(0.0003200000000000002); expect(fastPowering(-5, 5)).toBe(-3125); - expect(fastPowering(-5, -5)).toBe(0.0003200000000000002); + expect(fastPowering(-5, -5)).toBe(-0.0003200000000000002); }); });