-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Extended Eucliedian Algorithm added to Maths folder #799
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe56f54
Added Extended Euclidean Algorithm (ExtendedEuclideanGCD.js) to Maths…
VinWare 5ccfafe
`npm run style` result
VinWare 56b29ca
Merge branch 'TheAlgorithms:master' into master
VinWare 79c7ce4
Fixed `!=` to `!==` and ran `npm run style`
VinWare e92e2e3
Added a short explanation of the Ext Euc Algo
VinWare File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// Foot: https://en.wikipedia.org/wiki/Foot_(unit) | ||
const feetToMeter = (feet) => { | ||
return feet*0.3048; | ||
return feet * 0.3048 | ||
} | ||
|
||
const meterToFeet = (meter) => { | ||
return meter/0.3048; | ||
return meter / 0.3048 | ||
} | ||
|
||
export { feetToMeter, meterToFeet } | ||
export { feetToMeter, meterToFeet } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Problem statement and explanation: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm | ||
* | ||
* This algorithm plays an important role for modular arithmetic, and by extension for cyptography algorithms | ||
* | ||
* Basic explanation: | ||
* The Extended Euclidean algorithm is a modification of the standard Euclidean GCD algorithm. | ||
* It allows to calculate coefficients x and y for the equation: | ||
* ax + by = gcd(a,b) | ||
* | ||
* This is called Bézout's identity and the coefficients are called Bézout coefficients | ||
* | ||
* The algorithm uses the Euclidean method of getting remainder: | ||
* r_i+1 = r_i-1 - qi*ri | ||
* and applies it to series s and t (with same quotient q at each stage) | ||
* When r_n reaches 0, the value r_n-1 gives the gcd, and s_n-1 and t_n-1 give the coefficients | ||
* | ||
* This implementation uses an iterative approach to calculate the values | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {Number} arg1 first argument | ||
* @param {Number} arg2 second argument | ||
* @returns Array with GCD and first and second Bézout coefficients | ||
*/ | ||
const extendedEuclideanGCD = (arg1, arg2) => { | ||
if (typeof arg1 !== 'number' || typeof arg2 !== 'number') throw new TypeError('Not a Number') | ||
if (arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers') | ||
|
||
// Make the order of coefficients correct, as the algorithm assumes r0 > r1 | ||
if (arg1 < arg2) { | ||
const res = extendedEuclideanGCD(arg2, arg1) | ||
const temp = res[1] | ||
res[1] = res[2] | ||
res[2] = temp | ||
return res | ||
} | ||
|
||
// At this point arg1 > arg2 | ||
|
||
// Remainder values | ||
let r0 = arg1 | ||
let r1 = arg2 | ||
|
||
// Coefficient1 values | ||
let s0 = 1 | ||
let s1 = 0 | ||
|
||
// Coefficient 2 values | ||
let t0 = 0 | ||
let t1 = 1 | ||
|
||
while (r1 !== 0) { | ||
const q = Math.floor(r0 / r1) | ||
|
||
const r2 = r0 - r1 * q | ||
const s2 = s0 - s1 * q | ||
const t2 = t0 - t1 * q | ||
|
||
r0 = r1 | ||
r1 = r2 | ||
s0 = s1 | ||
s1 = s2 | ||
t0 = t1 | ||
t1 = t2 | ||
} | ||
return [r0, s0, t0] | ||
} | ||
|
||
export { extendedEuclideanGCD } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { extendedEuclideanGCD } from '../ExtendedEuclideanGCD' | ||
|
||
describe('extendedEuclideanGCD', () => { | ||
it('should return valid values in order for positive arguments', () => { | ||
expect(extendedEuclideanGCD(240, 46)).toMatchObject([2, -9, 47]) | ||
expect(extendedEuclideanGCD(46, 240)).toMatchObject([2, 47, -9]) | ||
}) | ||
it('should give error on non-positive arguments', () => { | ||
expect(() => extendedEuclideanGCD(0, 240)).toThrowError(new TypeError('Must be positive numbers')) | ||
expect(() => extendedEuclideanGCD(46, -240)).toThrowError(new TypeError('Must be positive numbers')) | ||
}) | ||
it('should give error on non-numeric arguments', () => { | ||
expect(() => extendedEuclideanGCD('240', 46)).toThrowError(new TypeError('Not a Number')) | ||
expect(() => extendedEuclideanGCD([240, 46])).toThrowError(new TypeError('Not a Number')) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.