Find the inverse of a number modulo a prime

前端 未结 3 1176
谎友^
谎友^ 2020-12-09 23:56

I was thinking about an algorithm to solve the congruence ax = 1 mod p with p prime. I was thinking about using Fermat theorem. Since I know that

3条回答
  •  佛祖请我去吃肉
    2020-12-10 00:38

    There is no reason this isn't a good algorithm for computers, you just need to be careful about the implementation, which isn't exactly trivial I guess, but it's not difficult either.

    Just use exponentiation by squaring, then it most likely won't matter how big p is.

    a^n = a^(n / 2) * a^(n / 2) for n even
        = a*a^(n - 1) for n odd
    

提交回复
热议问题