Skip to content

Commit 6886249

Browse files
author
Star-325
committed
Added Extended Euclidean Algorithm (ExtendedEuclideanGCD.js) to Maths folder, along with relevant test
1 parent dccd487 commit 6886249

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Maths/ExtendedEuclideanGCD.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Problem statement and explanation: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
3+
*
4+
* This algorithm plays an important role for modular arithmetic, and by extension for cyptography algorithms
5+
*
6+
* This implementation uses an iterative approach to calculate
7+
*/
8+
9+
/**
10+
*
11+
* @param {Number} arg1 first argument
12+
* @param {Number} arg2 second argument
13+
* @returns Array with GCD and first and second Bézout coefficients
14+
*/
15+
const extendedEuclideanGCD = (arg1, arg2) => {
16+
if(typeof arg1 != 'number' || typeof arg2 != 'number') throw new TypeError('Not a Number');
17+
if(arg1 < 1 || arg2 < 1) throw new TypeError('Must be positive numbers');
18+
19+
// Make the order of coefficients correct, as the algorithm assumes r0 > r1
20+
if (arg1 < arg2) {
21+
const res = extendedEuclideanGCD(arg2,arg1)
22+
const temp = res[1]
23+
res[1] = res[2]
24+
res[2] = temp
25+
return res;
26+
}
27+
28+
// At this point arg1 > arg2
29+
30+
// Remainder values
31+
let r0 = arg1
32+
let r1 = arg2
33+
34+
// Coefficient1 values
35+
let s0 = 1
36+
let s1 = 0
37+
38+
// Coefficient 2 values
39+
let t0 = 0
40+
let t1 = 1
41+
42+
while(r1 != 0) {
43+
const q = Math.floor(r0 / r1);
44+
45+
const r2 = r0 - r1*q;
46+
const s2 = s0 - s1*q;
47+
const t2 = t0 - t1*q;
48+
49+
r0 = r1
50+
r1 = r2
51+
s0 = s1
52+
s1 = s2
53+
t0 = t1
54+
t1 = t2
55+
}
56+
return [r0,s0,t0];
57+
}
58+
59+
export { extendedEuclideanGCD };
60+
// ex
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { extendedEuclideanGCD } from '../ExtendedEuclideanGCD'
2+
3+
describe('extendedEuclideanGCD', () => {
4+
it('should return valid values in order for positive arguments', () => {
5+
expect(extendedEuclideanGCD(240, 46)).toMatchObject([2, -9, 47])
6+
expect(extendedEuclideanGCD(46, 240)).toMatchObject([2, 47, -9])
7+
})
8+
it('should give error on non-positive arguments', () => {
9+
expect(() => extendedEuclideanGCD(0,240)).toThrowError(new TypeError('Must be positive numbers'))
10+
expect(() => extendedEuclideanGCD(46,-240)).toThrowError(new TypeError('Must be positive numbers'))
11+
})
12+
it('should give error on non-numeric arguments', () => {
13+
expect(() => extendedEuclideanGCD('240',46)).toThrowError(new TypeError('Not a Number'));
14+
expect(() => extendedEuclideanGCD([240,46])).toThrowError(new TypeError('Not a Number'));
15+
})
16+
})

0 commit comments

Comments
 (0)