-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindInverse.js
47 lines (36 loc) · 871 Bytes
/
findInverse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Finding an inverse modulo n
*
* Input: natural number n > 1, natural number 0 ≤ a < n
*
* Output:
* a message that GCD(a, n) > 1
* or
* a number 0 ≤ b < n such that n ∣ (ab − 1).
*
* WARNING: empty list [] on output means that there was no inverse.
*/
const utilities = require('../utilities');
function recInversion(a, n, b, c) {
if (a % n == 0) {
b = 0;
c = 1;
return;
}
recInversion(n, a % n, b, c);
let temp = b;
b = c;
c = temp - c * (a / n);
}
function findInverse(a, n) {
let A = utilities.convertToDecimal(a.join(''));
let N = utilities.convertToDecimal(n.join(''));
if (utilities.GCD(A, N) > 1) {
return [];
}
let b, c;
recInversion(A, N, b, c);
if (b < 0) b += N;
return utilities.convertToBinary(b);
}
module.exports = findInverse;