y-1 x^y = x * x^(y-1). , , y 1 y-1.
- y "". y, x^y = x^(2*y/2) = (x^2)^(y/2). y, x^y = x^(2*y/2+1) = x * (x^2)^(y/2).
, y, x^2 x.
:
Power(x, y)=
1 if y = 0
x if y = 1
Power(x * x, y / 2) if y even
x * Power(x * x, y / 2) if y odd
y . y = b0 + 2.b1 + 4.b2 + 8.b3...
From the properties of exposure it follows:
x^y = x^b0 . x^(2.b1) . x^(4.b2) . x^(8.b2)...
= x^b0 . (x^2)^b1 . (x^4)^b2 . (x^8)^b3...
You can get the desired powers of x by squaring, and the binary expansion of y tells you which powers to multiply.
source
share