The all one polynomial modulus is
sum(x**i for i in range(n)) % m
In my case 1 < x < m
, 0 < n < m
and all are integer. The sum can be simplified to (x**n - 1) / (x - 1)
. Due to numerical issues integer division needs to be used for larger n
. So
(x**n - 1) // (x - 1) % m
is equivalent. Problem is, the power can become quite huge, although Python handles this nicely it gets slow. Python’s 3 argument pow(x,n,m)
-function can efficiently calculate powers and modulus in one step, but it’s unclear how to make use of it here. The key is probably a transformation using modulus arithmetics.