You are given an integer base a and a non-negative integer exponent b. Your task is to calculate and return a raised to the power b efficiently using exponentiation by squaring.
Exponentiation by squaring reduces the number of multiplications required by repeatedly squaring the base and dividing the exponent by 2. This approach efficiently computes the result without performing b individual multiplications.
Input: a = 2, b = 10
Output: 1024
Explanation: 2 raised to the power 10 is 1024. Exponentiation by squaring computes the result through repeated squaring.
Input: a = 3, b = 0
Output: 1
Explanation: Any non-zero number raised to the power 0 is 1.
Input: a = 5, b = 3
Output: 125
Explanation: 5 raised to the power 3 is 5 * 5 * 5 = 125.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
2 10
1024
3 5
243