Guest User

Untitled

a guest
Jun 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.31 KB | None | 0 0
  1. /*
  2. given a and m, function finds x for a * x = 1 (mod m)
  3.  
  4. 24 * x = 1 (mod 5)
  5. 24 = x*exp(-1) (mod 5)
  6. x = 4
  7.  
  8. modInverse(a: 24, m: 5) returns optional(4)
  9. */
  10.  
  11. func modInverse(a: Int, m: Int) -> Int? {
  12. let tmp = a % m
  13. for i in 1..<m {
  14. if (tmp * i % m == 1) {
  15. return i;
  16. }
  17. }
  18. return nil
  19. }
Add Comment
Please, Sign In to add comment