Russian Peasant Exponentiation

  • + 0 comments

    Python: This solution works for values of k which do not lead to overflow when calculating: a^k and similarly large values.

    def solve(a, b, k, m):
        complex_num = complex(a, b) ** k
        lst = map(int, [complex_num.real, complex_num.imag])
        return list(map(lambda x: x % m, lst))