Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Iterative Function to calculate (x^y) in O(log y) */
- int power(int x, unsigned int y)
- {
- int res = 1;
- while (y > 0)
- {
- // If y is odd, multiply x with result
- if (y & 1)
- res = res*x;
- // y must be even now
- y = y>>1; // y = y/2
- x = x*x; // Change x to x^2
- }
- return res;
- }
Add Comment
Please, Sign In to add comment