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
Updated few Files
amitsingh19975 committed Sep 4, 2018
commit 71680c5b1d9b6a8274b6d4cb546b31d2639b0eb1
27 changes: 8 additions & 19 deletions src/algorithms/math/bits/__test__/isEven.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion src/algorithms/math/nth-root/README.MD
Original file line number Diff line number Diff line change
@@ -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)
- [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_algorithm)
2 changes: 1 addition & 1 deletion src/algorithms/math/nth-root/__test__/nthRoot.test.js
Original file line number Diff line number Diff line change
@@ -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);
38 changes: 24 additions & 14 deletions src/algorithms/math/nth-root/nthRoot.js
Original file line number Diff line number Diff line change
@@ -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);
}