Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- given a and m, function finds x for a * x = 1 (mod m)
- 24 * x = 1 (mod 5)
- 24 = x*exp(-1) (mod 5)
- x = 4
- modInverse(a: 24, m: 5) returns optional(4)
- */
- func modInverse(a: Int, m: Int) -> Int? {
- let tmp = a % m
- for i in 1..<m {
- if (tmp * i % m == 1) {
- return i;
- }
- }
- return nil
- }
Add Comment
Please, Sign In to add comment