1 year ago

#235580

test-img

YoungCoder5

Efficient exponentiation with division

I need to calculate $\frac{a^p-1}{a-1}\mod m$ to compute a repeated application of a linear transform under a modulus.

((a ** p - 1) // (a - 1)) % m == ((pow(a, p, x) - 1) // (a - 1)) % m

How could I figure an x that would work given a, p, and m? Could it just be x = ((a - 1) * m)? They're all integers and m is prime. It's just that a ** p is simply far to large and for to slow to calculate, and it's unnecessary.

I'm trying to compute the composition of linear transforms. Say there are two functions:

A = lambda x: (a * x + b) % m
B = lambda x: (c * x + d) % m

Composing them is simple. But calculating the repeated application of A is what I'm trying to do. Define the function C as A(x) == C(A, 1)(x), A(A(x)) == C(A, 2)(x), A(A(A(x))) == C(A, 3)(x), and so on. B works too.

C(A, n)(x) == (pow(a, n, m) * x + b * ((a ** n - 1) // (a - 1))) % m
C(B, n)(x) == (pow(c, n, m) * x + d * ((c ** n - 1) // (c - 1))) % m

I'm not trying to back-calculate A. I'm trying to forward calculate C.

python

performance

math

exponentiation

0 Answers

Your Answer

Accepted video resources