Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended Euclidean Algorithm #200

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added nth root of number
amitsingh19975 committed Sep 3, 2018
commit 6ec6e7cb3a5ea11e51317ffa99a98d75a674470a
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)
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.259921049894873);
expect(nthRoot(2, 5)).toBeCloseTo(1.148698354997035);
expect(nthRoot(2, 6)).toBeCloseTo(1.122462048309373);
expect(nthRoot(2, 7)).toBeCloseTo(1.104089513673812);
});
});
22 changes: 22 additions & 0 deletions src/algorithms/math/nth-root/nthRoot.js
Original file line number Diff line number Diff line change
@@ -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;
}
}