Skip to content

Commit 2b8e4c5

Browse files
authored
Create inverse.js
1 parent 0283953 commit 2b8e4c5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/inverse.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function inverse(a, mod) {
2+
return quickPow(a, mod - 2, mod) % mod
3+
}
4+
5+
function quickPow(a, b, p = mod) {
6+
let ans = 1;
7+
a = (a % p + p) % p;
8+
for (; b; b >>= 1) {
9+
if (b & 1) ans = (a * ans) % p;
10+
a = (a * a) % p;
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)