Skip to content

Instantly share code, notes, and snippets.

@adist98
Created June 16, 2019 11:24
Show Gist options
  • Save adist98/87a0abe076cfef52d6e2ab3924127c24 to your computer and use it in GitHub Desktop.
Save adist98/87a0abe076cfef52d6e2ab3924127c24 to your computer and use it in GitHub Desktop.
/* Function to calculate x raised to the power y in O(logn)*/
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
@adist98
Copy link
Author

adist98 commented Jun 16, 2019

This function calculates only positive powers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment