From df8942f21e8130af42679886840718b0518dac04 Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Mon, 3 Sep 2018 21:20:27 +0530
Subject: [PATCH 1/8] Improved the countSetBit

---
 src/algorithms/math/bits/countSetBits.js | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/algorithms/math/bits/countSetBits.js b/src/algorithms/math/bits/countSetBits.js
index 6e24eebf26..28a2204d96 100644
--- a/src/algorithms/math/bits/countSetBits.js
+++ b/src/algorithms/math/bits/countSetBits.js
@@ -7,11 +7,11 @@ export default function countSetBits(originalNumber) {
   let number = originalNumber;
 
   while (number) {
-    // Add last bit of the number to the sum of set bits.
-    setBitsCount += number & 1;
+    // Using And operation on number with previous number.
+    number &= (number - 1);
 
-    // Shift number right by one bit to investigate other bits.
-    number >>= 1;
+    // Increamenting number by 1.
+    setBitsCount += 1;
   }
 
   return setBitsCount;

From e6ee494c20ba0ed616c48fd98fca0c7271ff63d1 Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Mon, 3 Sep 2018 21:38:45 +0530
Subject: [PATCH 2/8] Added if number is Even or Odd

---
 .../math/bits/__test__/isEven.test.js         | 30 +++++++++++++++++++
 src/algorithms/math/bits/isEven.js            |  8 +++++
 2 files changed, 38 insertions(+)
 create mode 100644 src/algorithms/math/bits/__test__/isEven.test.js
 create mode 100644 src/algorithms/math/bits/isEven.js

diff --git a/src/algorithms/math/bits/__test__/isEven.test.js b/src/algorithms/math/bits/__test__/isEven.test.js
new file mode 100644
index 0000000000..0746a1629b
--- /dev/null
+++ b/src/algorithms/math/bits/__test__/isEven.test.js
@@ -0,0 +1,30 @@
+import isEven from '../isEven';
+
+describe('isEven', () => {
+  it('should get if number is even or not', () => {
+    // 2 = true
+    // 3 = false
+    expect(isEven(2)).toBe(true);
+    expect(isEven(3)).toBe(false);
+
+    // 5 = odd
+    // 6 = even
+    expect(isEven(5)).toBe(false);
+    expect(isEven(6)).toBe(true);
+
+    // 7 = odd
+    // 8 = even
+    expect(isEven(7)).toBe(false);
+    expect(isEven(8)).toBe(true);
+
+    // 9 = odd
+    // 10 = even
+    expect(isEven(9)).toBe(false);
+    expect(isEven(10)).toBe(true);
+
+    // 11 = odd
+    // 12 = even
+    expect(isEven(11)).toBe(false);
+    expect(isEven(12)).toBe(true);
+  });
+});
diff --git a/src/algorithms/math/bits/isEven.js b/src/algorithms/math/bits/isEven.js
new file mode 100644
index 0000000000..ab873bbca6
--- /dev/null
+++ b/src/algorithms/math/bits/isEven.js
@@ -0,0 +1,8 @@
+/**
+ * @param {number} number
+ * @param {number} bitPosition - zero based.
+ * @return {number}
+ */
+export default function isEven(number) {
+  return ((number & 1) === 0);
+}

From 55240dfe5e0a463bc123bcbdb76ef0ad6eb3426c Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Mon, 3 Sep 2018 21:39:20 +0530
Subject: [PATCH 3/8] Added if number is Even or Odd

---
 src/algorithms/math/bits/isEven.js | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/algorithms/math/bits/isEven.js b/src/algorithms/math/bits/isEven.js
index ab873bbca6..2bbb9e587c 100644
--- a/src/algorithms/math/bits/isEven.js
+++ b/src/algorithms/math/bits/isEven.js
@@ -1,7 +1,6 @@
 /**
  * @param {number} number
- * @param {number} bitPosition - zero based.
- * @return {number}
+ * @return {Boolean}
  */
 export default function isEven(number) {
   return ((number & 1) === 0);

From 6ec6e7cb3a5ea11e51317ffa99a98d75a674470a Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Mon, 3 Sep 2018 22:54:36 +0530
Subject: [PATCH 4/8] Added nth root of number

---
 README.md                                     |  1 +
 src/algorithms/math/nth-root/README.MD        |  8 +++++++
 .../math/nth-root/__test__/nthRoot.test.js    | 12 ++++++++++
 src/algorithms/math/nth-root/nthRoot.js       | 22 +++++++++++++++++++
 4 files changed, 43 insertions(+)
 create mode 100644 src/algorithms/math/nth-root/README.MD
 create mode 100644 src/algorithms/math/nth-root/__test__/nthRoot.test.js
 create mode 100644 src/algorithms/math/nth-root/nthRoot.js

diff --git a/README.md b/README.md
index 915b3fb38e..9f125466cc 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,7 @@ a set of rules that precisely define a sequence of operations.
 * **Math**
   * `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two, make negative etc.
   * `B` [Factorial](src/algorithms/math/factorial) 
+  * `B` [nth Root](src/algorithms/math/nth-root)
   * `B` [Fibonacci Number](src/algorithms/math/fibonacci)
   * `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
   * `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
diff --git a/src/algorithms/math/nth-root/README.MD b/src/algorithms/math/nth-root/README.MD
new file mode 100644
index 0000000000..f282b09259
--- /dev/null
+++ b/src/algorithms/math/nth-root/README.MD
@@ -0,0 +1,8 @@
+# Nth Power using Binary Search
+
+In computer science, `binary search`, also known as `half-interval search`, `logarithmic search`, or `binary chop`,is a search algorithm that finds the position of a target value within a range or sorted array. Binary search compares the target value to the middle element of the range or array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. Even though the idea is simple, implementing binary search correctly requires attention to some subtleties about its exit conditions and midpoint calculation.
+
+![Nth Root](https://upload.wikimedia.org/wikipedia/commons/8/83/Binary_Search_Depiction.svg)
+
+## References
+[Wikipedia](https://en.wikipedia.org/wiki/Binary_search_algorithm)
\ No newline at end of file
diff --git a/src/algorithms/math/nth-root/__test__/nthRoot.test.js b/src/algorithms/math/nth-root/__test__/nthRoot.test.js
new file mode 100644
index 0000000000..1e07f0afe4
--- /dev/null
+++ b/src/algorithms/math/nth-root/__test__/nthRoot.test.js
@@ -0,0 +1,12 @@
+import nthRoot from '../nthRoot';
+
+describe('nth Root', () => {
+  it('should nth root of number', () => {
+    expect(nthRoot(2, 2)).toBeCloseTo(1.414213562373095);
+    expect(nthRoot(2, 3)).toBeCloseTo(1.259921049894873);
+    expect(nthRoot(2, 4)).toBeCloseTo(1.259921049894873);
+    expect(nthRoot(2, 5)).toBeCloseTo(1.148698354997035);
+    expect(nthRoot(2, 6)).toBeCloseTo(1.122462048309373);
+    expect(nthRoot(2, 7)).toBeCloseTo(1.104089513673812);
+  });
+});
diff --git a/src/algorithms/math/nth-root/nthRoot.js b/src/algorithms/math/nth-root/nthRoot.js
new file mode 100644
index 0000000000..b32db8d628
--- /dev/null
+++ b/src/algorithms/math/nth-root/nthRoot.js
@@ -0,0 +1,22 @@
+/**
+ * @param {number} number
+ * @param {number} power
+ * @return {number}
+ */
+
+function difference(num, mid, p) {
+  return Math.abs(num - (mid ** p));
+}
+
+export default function nthRoot(number, p) {
+  let start = 0.0;
+  let end = number;
+  const e = 0.000000001;
+  while (true) {
+    const mid = (start + end) / 2;
+    const error = difference(number, mid, p);
+    if (error <= e) return mid;
+    if (mid ** p > number) end = mid;
+    else start = mid;
+  }
+}

From b9a8347fe0800ddef8186d6cf87d64b3d47b47c9 Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Tue, 4 Sep 2018 17:39:21 +0530
Subject: [PATCH 5/8] counting set bit Optimized

---
 src/algorithms/math/bits/countSetBits.js | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/algorithms/math/bits/countSetBits.js b/src/algorithms/math/bits/countSetBits.js
index 28a2204d96..f054ac03fa 100644
--- a/src/algorithms/math/bits/countSetBits.js
+++ b/src/algorithms/math/bits/countSetBits.js
@@ -10,7 +10,6 @@ export default function countSetBits(originalNumber) {
     // Using And operation on number with previous number.
     number &= (number - 1);
 
-    // Increamenting number by 1.
     setBitsCount += 1;
   }
 

From 71680c5b1d9b6a8274b6d4cb546b31d2639b0eb1 Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Tue, 4 Sep 2018 18:08:10 +0530
Subject: [PATCH 6/8] Updated few Files

---
 .../math/bits/__test__/isEven.test.js         | 27 ++++---------
 src/algorithms/math/nth-root/README.MD        |  2 +-
 .../math/nth-root/__test__/nthRoot.test.js    |  2 +-
 src/algorithms/math/nth-root/nthRoot.js       | 38 ++++++++++++-------
 4 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/src/algorithms/math/bits/__test__/isEven.test.js b/src/algorithms/math/bits/__test__/isEven.test.js
index 0746a1629b..d9f6946526 100644
--- a/src/algorithms/math/bits/__test__/isEven.test.js
+++ b/src/algorithms/math/bits/__test__/isEven.test.js
@@ -1,30 +1,19 @@
 import isEven from '../isEven';
 
 describe('isEven', () => {
-  it('should get if number is even or not', () => {
-    // 2 = true
-    // 3 = false
+  it('should get if number is even', () => {
     expect(isEven(2)).toBe(true);
-    expect(isEven(3)).toBe(false);
-
-    // 5 = odd
-    // 6 = even
-    expect(isEven(5)).toBe(false);
     expect(isEven(6)).toBe(true);
-
-    // 7 = odd
-    // 8 = even
-    expect(isEven(7)).toBe(false);
     expect(isEven(8)).toBe(true);
-
-    // 9 = odd
-    // 10 = even
-    expect(isEven(9)).toBe(false);
     expect(isEven(10)).toBe(true);
+    expect(isEven(12)).toBe(true);
+  });
 
-    // 11 = odd
-    // 12 = even
+  it('should get if number is odd', () => {
+    expect(isEven(3)).toBe(false);
+    expect(isEven(5)).toBe(false);
+    expect(isEven(7)).toBe(false);
+    expect(isEven(9)).toBe(false);
     expect(isEven(11)).toBe(false);
-    expect(isEven(12)).toBe(true);
   });
 });
diff --git a/src/algorithms/math/nth-root/README.MD b/src/algorithms/math/nth-root/README.MD
index f282b09259..2a371a9fbd 100644
--- a/src/algorithms/math/nth-root/README.MD
+++ b/src/algorithms/math/nth-root/README.MD
@@ -5,4 +5,4 @@ In computer science, `binary search`, also known as `half-interval search`, `log
 ![Nth Root](https://upload.wikimedia.org/wikipedia/commons/8/83/Binary_Search_Depiction.svg)
 
 ## References
-[Wikipedia](https://en.wikipedia.org/wiki/Binary_search_algorithm)
\ No newline at end of file
+- [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_algorithm)
\ No newline at end of file
diff --git a/src/algorithms/math/nth-root/__test__/nthRoot.test.js b/src/algorithms/math/nth-root/__test__/nthRoot.test.js
index 1e07f0afe4..987b6d9ba2 100644
--- a/src/algorithms/math/nth-root/__test__/nthRoot.test.js
+++ b/src/algorithms/math/nth-root/__test__/nthRoot.test.js
@@ -4,7 +4,7 @@ describe('nth Root', () => {
   it('should nth root of number', () => {
     expect(nthRoot(2, 2)).toBeCloseTo(1.414213562373095);
     expect(nthRoot(2, 3)).toBeCloseTo(1.259921049894873);
-    expect(nthRoot(2, 4)).toBeCloseTo(1.259921049894873);
+    expect(nthRoot(2, 4)).toBeCloseTo(1.189207115);
     expect(nthRoot(2, 5)).toBeCloseTo(1.148698354997035);
     expect(nthRoot(2, 6)).toBeCloseTo(1.122462048309373);
     expect(nthRoot(2, 7)).toBeCloseTo(1.104089513673812);
diff --git a/src/algorithms/math/nth-root/nthRoot.js b/src/algorithms/math/nth-root/nthRoot.js
index b32db8d628..584673a30e 100644
--- a/src/algorithms/math/nth-root/nthRoot.js
+++ b/src/algorithms/math/nth-root/nthRoot.js
@@ -1,22 +1,32 @@
 /**
- * @param {number} number
+ * @param {number} num
  * @param {number} power
  * @return {number}
  */
 
-function difference(num, mid, p) {
-  return Math.abs(num - (mid ** p));
-}
+export default function nthRoot(num, power) {
+  const E = 0.000000001;
+  const roundToMargin = (x) => {
+    return Math.round(x / E) * E;
+  };
+
+  let guess;
+  const calculateError = () => Math.abs(num - (guess ** power));
+
+  let start = 0;
+  let end = num;
+  let error = 1;
 
-export default function nthRoot(number, p) {
-  let start = 0.0;
-  let end = number;
-  const e = 0.000000001;
-  while (true) {
-    const mid = (start + end) / 2;
-    const error = difference(number, mid, p);
-    if (error <= e) return mid;
-    if (mid ** p > number) end = mid;
-    else start = mid;
+  while (error > E) {
+    guess = (start + end) / 2;
+    error = calculateError();
+
+    if (guess ** power > num) {
+      end = guess;
+    } else {
+      start = guess;
+    }
   }
+
+  return roundToMargin(guess);
 }

From 449e6fcdc79bdf6247c1619582336022e8a594a7 Mon Sep 17 00:00:00 2001
From: Amit Singh <amitsingh19975@gmail.com>
Date: Tue, 4 Sep 2018 19:30:54 +0530
Subject: [PATCH 7/8] Added Extended Euclidean Algorithm

---
 .../extended-euclidean-algorithm/README.MD    | 14 +++++++++++
 .../__test__/extendedEuclid.test.js           | 23 +++++++++++++++++++
 .../extendedEuclid.js                         | 21 +++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 src/algorithms/math/extended-euclidean-algorithm/README.MD
 create mode 100644 src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclid.test.js
 create mode 100644 src/algorithms/math/extended-euclidean-algorithm/extendedEuclid.js

diff --git a/src/algorithms/math/extended-euclidean-algorithm/README.MD b/src/algorithms/math/extended-euclidean-algorithm/README.MD
new file mode 100644
index 0000000000..24f9c654bb
--- /dev/null
+++ b/src/algorithms/math/extended-euclidean-algorithm/README.MD
@@ -0,0 +1,14 @@
+# Extended Euclidean algorithm
+
+In arithmetic and computer programming, the extended Euclidean algorithm is an extension to the Euclidean algorithm,
+and computes, in addition to the greatest common divisor of integers a and b, also the coefficients of Bézout's 
+identity, which are integers x and y such that `ax + by = gcd(a,b)`.
+This is a certifying algorithm, because the gcd is the only number that can simultaneously satisfy this equation and 
+divide the inputs. It allows one to compute also, with almost no extra cost, the quotients of a and b by their 
+greatest common divisor.
+
+![Extended](https://i.stack.imgur.com/UiQ2m.png)
+
+## References
+
+[Wikipedia](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm)
diff --git a/src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclid.test.js b/src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclid.test.js
new file mode 100644
index 0000000000..967876c954
--- /dev/null
+++ b/src/algorithms/math/extended-euclidean-algorithm/__test__/extendedEuclid.test.js
@@ -0,0 +1,23 @@
+import extendedEuclid from '../extendedEuclid';
+
+describe('extendedEuclideanAlgorithm', () => {
+  it('should calculate coefficients', () => {
+    expect(extendedEuclid(30, 20)).toEqual({
+      gcd: 10,
+      x: 1,
+      y: -1,
+    });
+
+    expect(extendedEuclid(30, 50)).toEqual({
+      gcd: 10,
+      x: 2,
+      y: -1,
+    });
+
+    expect(extendedEuclid(65, 40)).toEqual({
+      gcd: 5,
+      x: -3,
+      y: 5,
+    });
+  });
+});
diff --git a/src/algorithms/math/extended-euclidean-algorithm/extendedEuclid.js b/src/algorithms/math/extended-euclidean-algorithm/extendedEuclid.js
new file mode 100644
index 0000000000..912c9c3607
--- /dev/null
+++ b/src/algorithms/math/extended-euclidean-algorithm/extendedEuclid.js
@@ -0,0 +1,21 @@
+/**
+ * @param {number} Coefficient A
+ * @param {number} Coefficient B
+ * @return {Object} {gcd, x, y}
+ */
+
+export default function extendedEuclid(coeffA, coeffB) {
+  if (coeffA === 0) {
+    return {
+      gcd: coeffB,
+      x: 0,
+      y: 1,
+    };
+  }
+  const variables = extendedEuclid(coeffB % coeffA, coeffA);
+  return {
+    gcd: variables.gcd,
+    x: (variables.y - Math.floor(coeffB / coeffA) * variables.x),
+    y: variables.x,
+  };
+}

From 5914879e797572137de9c87a73928ef61450023b Mon Sep 17 00:00:00 2001
From: amitsingh19975 <amitsingh19975@gmail.com>
Date: Tue, 4 Sep 2018 19:40:10 +0530
Subject: [PATCH 8/8] Update README.md

---
 README.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/README.md b/README.md
index 9f125466cc..a62984fb7c 100644
--- a/README.md
+++ b/README.md
@@ -59,6 +59,7 @@ a set of rules that precisely define a sequence of operations.
   * `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two, make negative etc.
   * `B` [Factorial](src/algorithms/math/factorial) 
   * `B` [nth Root](src/algorithms/math/nth-root)
+  * `B` [Extended Euclidean Algorithm](src/algorithms/math/extended-euclidean-algorithm)
   * `B` [Fibonacci Number](src/algorithms/math/fibonacci)
   * `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
   * `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)