Pergunta de entrevista da empresa Google

Write a program to find (x^y) % z

Respostas da entrevista

Sigiloso

19 de fev. de 2011

I guess the point is how to avoid overflow. Good algorithms for this does not lead to overflow and return the correct answer; normal ones may easily lead to overflow.

1

Sigiloso

15 de jun. de 2012

I guess we need to use Congruences from Number theory here

Sigiloso

13 de jan. de 2011

I think there is some point we need to check. first will x be negative? If so, we need to handle this problem In addition, in the case y is very large, a O(y) algorithm is not a good idea, we might need to use the O(lg Y) one.

Sigiloso

12 de jan. de 2011

result = x mod z for i = 2 to y do result = (result * x) mod z

Sigiloso

7 de fev. de 2011

(x ^ y) % z is equal to: (x << (y-1)) % z