0

http://en.wikipedia.org/wiki/Modular_exponentiation#Right-to-left_binary_method

What is the time complexity of the right to left method in modular exponentiation?

is it O(1) because we are going through the binary representation of e which is constant time? assuming e is a 32-bit integer

int mod(int b, int e, int m) {
    b = b % m;
    int result = 1;

    while (e) {
        if (e % 2 == 1) {
            result = (result * b) % m;
        }
        b = (b * b) % m;
        e >>= 1;
    }
    return result;
}
test
  • 101
  • 2
    If you assume everything's a 32-bit integer, you have a finite problem, since there are only finitely many values of $b$, $e$ and $m$ that you could be interested in. All finite problems are constant time because, in principle, you could just make a big look-up table containing all the answers. So, to get a meaningful answer to your question, you'll have to be more careful about what model of computation you're using. – David Richerby Feb 01 '15 at 21:25
  • Can you give an example? – test Feb 01 '15 at 22:16

0 Answers0