Skip to content

Commit 9767621

Browse files
committed
Merge pull request #37 from quietshu/master
Extended Euclidean algorithm
2 parents 4350b94 + b11e579 commit 9767621

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

Diff for: algorithms/math/extended_euclidean.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (C) 2014 Shu Ding
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
'use strict';
24+
25+
/**
26+
* Extended Euclidean algorithm to calculate the solve of
27+
* ax + by = gcd(a, b)
28+
* gcd(a, b) is the greatest common divisor of integers a and b.
29+
*
30+
* @param Number
31+
* @param Number
32+
*
33+
* @return {Number, Number}
34+
*/
35+
var extEuclid = function (a, b) {
36+
var s = 0, oldS = 1;
37+
var t = 1, oldT = 0;
38+
var r = b, oldR = a;
39+
var quotient, temp;
40+
while (r !== 0) {
41+
quotient = Math.floor(oldR / r);
42+
43+
temp = r;
44+
r = oldR - quotient * r;
45+
oldR = temp;
46+
47+
temp = s;
48+
s = oldS - quotient * s;
49+
oldS = temp;
50+
51+
temp = t;
52+
t = oldT - quotient * t;
53+
oldT = temp;
54+
}
55+
56+
return {
57+
x: oldS,
58+
y: oldT
59+
};
60+
};
61+
62+
module.exports = extEuclid;

Diff for: main.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ var lib = {
3030
Math: {
3131
fibonacci: require('./algorithms/math/fibonacci'),
3232
fisherYates: require('./algorithms/math/fisher_yates'),
33-
gcd: require('./algorithms/math/gcd')
33+
gcd: require('./algorithms/math/gcd'),
34+
extendedEuclidean: require('./algorithms/math/extended_euclidean')
3435
},
3536
Search: {
3637
bfs: require('./algorithms/searching/bfs'),

Diff for: test/algorithms/math/extended_euclidean.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (C) 2014 Shu Ding
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
var extEuclid = require('../../../algorithms/math/extended_euclidean'),
25+
assert = require('assert');
26+
27+
describe('extEuclid', function () {
28+
it('should calculate the solve to Bézout\'s identity', function () {
29+
var solve = extEuclid(1, 0);
30+
assert.equal(solve.x, 1);
31+
assert.equal(solve.y, 0);
32+
33+
solve = extEuclid(25, 35);
34+
assert.equal(solve.x, 3);
35+
assert.equal(solve.y, -2);
36+
37+
solve = extEuclid(-55, 22);
38+
assert.equal(solve.x, 1);
39+
assert.equal(solve.y, 3);
40+
});
41+
});

0 commit comments

Comments
 (0)