I wrote an algorithm by combining Fermat's Little Theorem and Euler's Method. However, I am experiencing a problem in Euler's method.
For instance, If I take $(A, B, M)$ such that $A^B mod(M)$.
When the initial values are $(12341, 123141, 12313)$ they reduce to $(12341, 7113, 12313)$ However after this the program won't go further. The reason I believe is that when $$B < \phi(M)$$ the algorithm stops working. So simply to algorithm work properly we need larger $B$ but small $M$.
At this point, my question is there a way to reduce M to small numbers by some mathematical operations ?
def eulers_theorem(A, B, M):
totient = M
factors_of_M = [(i-1) / i for i in factorint(M).keys()]
for i in factors_of_M:
totient *= i
new_B = int(B % totient)
return (A, new_B, M)
So I am factorizing the M values and then calculating the totient of it( $\phi(M)$ ) and then My new B becomes
$B_{new} = B~mod (\phi(M))$
For instance, if (A, B, M) = (123, 562, 100)
$\phi(100) = 40$ so
$$B_{new} = 562~mod (40) = 2$$
So (123, 562, 100) reduces to (123, 2, 100)
In the Above example when $(12341, 123141, 12313)$ it reduces to $(12341, 7113, 12313)$. In this case the algorithm enters a loop since,
$$B_{new} = 7113~mod (\phi(12313)) = 7113~mod(10548) = 7113$$ so $B_{new}$ is always the same.
Apply Fermat's little theorem and extentions, for both of $p_1,p_2$, then apply Chinese remainder theorem ( with LCM extension if needed) to the exponents for each to find the intersection. Apply to B Solve mod M.