Solving modular linear congruences for large numbers
问题 I'm looking for a better algorithm than one I found on stackoverflow to handle 4096 byte numbers, i'm hitting a maximum recursion depth. Code from stackoverlow post, i copy/pasted it but lost the original link: def linear_congruence(a, b, m): if b == 0: return 0 if a < 0: a = -a b = -b b %= m while a > m: a -= m return (m * linear_congruence(m, -b, a) + b) // a This works fine for smaller numbers, for example: In [167]: pow_mod(8261, 63, 4033) 63 1 8261 4033 31 195 1728 4033 15 2221 1564 4033