Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: amitsingh19975/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 6 commits
  • 7 files changed
  • 1 contributor

Commits on Sep 3, 2018

  1. Copy the full SHA
    df8942f View commit details
  2. Copy the full SHA
    e6ee494 View commit details
  3. Copy the full SHA
    55240df View commit details
  4. Copy the full SHA
    6ec6e7c View commit details

Commits on Sep 4, 2018

  1. Copy the full SHA
    b9a8347 View commit details
  2. Updated few Files

    amitsingh19975 committed Sep 4, 2018
    Copy the full SHA
    71680c5 View commit details
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions src/algorithms/math/bits/__test__/isEven.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import isEven from '../isEven';

describe('isEven', () => {
it('should get if number is even', () => {
expect(isEven(2)).toBe(true);
expect(isEven(6)).toBe(true);
expect(isEven(8)).toBe(true);
expect(isEven(10)).toBe(true);
expect(isEven(12)).toBe(true);
});

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);
});
});
7 changes: 3 additions & 4 deletions src/algorithms/math/bits/countSetBits.js
Original file line number Diff line number Diff line change
@@ -7,11 +7,10 @@ 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;
setBitsCount += 1;
}

return setBitsCount;
7 changes: 7 additions & 0 deletions src/algorithms/math/bits/isEven.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {number} number
* @return {Boolean}
*/
export default function isEven(number) {
return ((number & 1) === 0);
}
8 changes: 8 additions & 0 deletions src/algorithms/math/nth-root/README.MD
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions src/algorithms/math/nth-root/__test__/nthRoot.test.js
Original file line number Diff line number Diff line change
@@ -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.189207115);
expect(nthRoot(2, 5)).toBeCloseTo(1.148698354997035);
expect(nthRoot(2, 6)).toBeCloseTo(1.122462048309373);
expect(nthRoot(2, 7)).toBeCloseTo(1.104089513673812);
});
});
32 changes: 32 additions & 0 deletions src/algorithms/math/nth-root/nthRoot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number} num
* @param {number} power
* @return {number}
*/

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;

while (error > E) {
guess = (start + end) / 2;
error = calculateError();

if (guess ** power > num) {
end = guess;
} else {
start = guess;
}
}

return roundToMargin(guess);
}