From ae3bd72ca84367849127119b2727472d6c24b14d Mon Sep 17 00:00:00 2001 From: Rahul Jain Date: Thu, 29 Oct 2020 21:36:31 +0530 Subject: [PATCH 1/3] Add Iterative Binary Exponentiation --- Maths/BinaryExponentiationIterative.js | 22 +++++++++++++++++++ .../BinaryExponentiationIterative.test.js | 15 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 Maths/BinaryExponentiationIterative.js create mode 100644 Maths/test/BinaryExponentiationIterative.test.js diff --git a/Maths/BinaryExponentiationIterative.js b/Maths/BinaryExponentiationIterative.js new file mode 100644 index 0000000000..9ce60705a8 --- /dev/null +++ b/Maths/BinaryExponentiationIterative.js @@ -0,0 +1,22 @@ +// To calculate x^n i.e. exponent(x, n) in O(log n) time in iterative way +// n is an integer and n >= 0 + +// Examples: +// 2^3 = 8 +// 5^0 = 1 + +// Uses the fact that +// exponent(x, n) +// = exponent(x*x, floor(n/2)) ; if n is odd +// = x*exponent(x*x, floor(n/2)) ; if n is even +const exponent = (x, n) => { + let ans = 1 + while(n > 0) { + if(n%2 != 0) ans *= x + n = Math.floor(n/2) + if(n > 0) x *= x + } + return ans +} + +export { exponent } \ No newline at end of file diff --git a/Maths/test/BinaryExponentiationIterative.test.js b/Maths/test/BinaryExponentiationIterative.test.js new file mode 100644 index 0000000000..ebd21cbac6 --- /dev/null +++ b/Maths/test/BinaryExponentiationIterative.test.js @@ -0,0 +1,15 @@ +import { exponent } from '../BinaryExponentiationIterative' + +describe('exponent', () => { + it('should return 1 when power is 0', () => { + expect(exponent(5, 0)).toBe(1) + }) + + it('should return 0 when base is 0', () => { + expect(exponent(0, 7)).toBe(0) + }) + + it('should return the value of a base raised to a power', () => { + expect(exponent(3, 5)).toBe(243) + }) +}) \ No newline at end of file From c7fe697ca40e6f12905849ad1772544e23970062 Mon Sep 17 00:00:00 2001 From: Rahul Jain Date: Thu, 29 Oct 2020 21:42:10 +0530 Subject: [PATCH 2/3] Add explanation for the Binary Exponentiation --- Maths/BinaryExponentiationIterative.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Maths/BinaryExponentiationIterative.js b/Maths/BinaryExponentiationIterative.js index 9ce60705a8..6a8bb77b0d 100644 --- a/Maths/BinaryExponentiationIterative.js +++ b/Maths/BinaryExponentiationIterative.js @@ -1,6 +1,8 @@ // To calculate x^n i.e. exponent(x, n) in O(log n) time in iterative way // n is an integer and n >= 0 +// Explanation: https://en.wikipedia.org/wiki/Exponentiation_by_squaring + // Examples: // 2^3 = 8 // 5^0 = 1 From 5e6d813b6f1ec0741c07a713c2b3d3ea9b9ec3ff Mon Sep 17 00:00:00 2001 From: Rahul Jain Date: Thu, 29 Oct 2020 21:54:07 +0530 Subject: [PATCH 3/3] Update Binary Exponentiation algorithm file to comply with Javascript standard rules --- Maths/BinaryExponentiationIterative.js | 16 +++++++-------- .../BinaryExponentiationIterative.test.js | 20 +++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Maths/BinaryExponentiationIterative.js b/Maths/BinaryExponentiationIterative.js index 6a8bb77b0d..705dcded4e 100644 --- a/Maths/BinaryExponentiationIterative.js +++ b/Maths/BinaryExponentiationIterative.js @@ -12,13 +12,13 @@ // = exponent(x*x, floor(n/2)) ; if n is odd // = x*exponent(x*x, floor(n/2)) ; if n is even const exponent = (x, n) => { - let ans = 1 - while(n > 0) { - if(n%2 != 0) ans *= x - n = Math.floor(n/2) - if(n > 0) x *= x - } - return ans + let ans = 1 + while (n > 0) { + if (n % 2 !== 0) ans *= x + n = Math.floor(n / 2) + if (n > 0) x *= x + } + return ans } -export { exponent } \ No newline at end of file +export { exponent } diff --git a/Maths/test/BinaryExponentiationIterative.test.js b/Maths/test/BinaryExponentiationIterative.test.js index ebd21cbac6..6e6fff8bd8 100644 --- a/Maths/test/BinaryExponentiationIterative.test.js +++ b/Maths/test/BinaryExponentiationIterative.test.js @@ -1,15 +1,15 @@ import { exponent } from '../BinaryExponentiationIterative' describe('exponent', () => { - it('should return 1 when power is 0', () => { - expect(exponent(5, 0)).toBe(1) - }) + it('should return 1 when power is 0', () => { + expect(exponent(5, 0)).toBe(1) + }) - it('should return 0 when base is 0', () => { - expect(exponent(0, 7)).toBe(0) - }) + it('should return 0 when base is 0', () => { + expect(exponent(0, 7)).toBe(0) + }) - it('should return the value of a base raised to a power', () => { - expect(exponent(3, 5)).toBe(243) - }) -}) \ No newline at end of file + it('should return the value of a base raised to a power', () => { + expect(exponent(3, 5)).toBe(243) + }) +})