Categories
algorithms kotlin

Problem 5.7 from EPI -> Compute X.power(Y)

This problem can be done in a brute force way by multiplying a number by itself y times. But there is a better approach, keep squaring X value and observing Y least significant bit, if y’s lsb is 1 take x value, bit-shift y value by 1 each time. This approach is faster since bit-shifting will converge to zero faster than doing y times squaring of the x value. Take a look at the code here.

Leave a Reply

Your email address will not be published. Required fields are marked *