java - Issue with inverse modulo where gcd(denominator,mod)!=1 -
how compute f(n)%mod
mod
prime number. , f(n)=n!/(q!^r)%mod
....(x^r
stands pow(x,r)
).
i'm trying fermat's little theorem computing inverse modulo problem i'm facing fermat applicable if gcd(denominator,mod)=1
.
so there other way solve this.
if modulus prime, can compute inverse using extended euclidean algorithm:
function inverse(x, m) a, b, u = 0, m, 1 while x > 0 q = b // x # integer partition x, a, b, u = b % x, u, x, - q * u if b == 1 homecoming % m error "must coprime"
if modulus composite, algorithm still work long x , m coprime. if share factor, inverse not exist.
java c algorithm math data-structures
No comments:
Post a Comment