Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Fast Exponentiation using Bit Manipulation

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given two integers A and N, the task is to calculate A raised to power N (i.e. AN).

Examples: 

Input: A = 3, N = 5 
Output: 243 
Explanation: 
3 raised to power 5 = (3*3*3*3*3) = 243

Input: A = 21, N = 4
Output: 194481 
Explanation: 
21 raised to power 4 = (21*21*21*21) = 194481 
 

Naive Approach: 
The simplest approach to solve this problem is to repetitively multiply A, N times and print the product. 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Efficient Approach: 
To optimize the above approach, the idea is to use Bit Manipulation. Convert the integer N to its binary form and follow the steps below:  

  • Initialize ans to store the final answer of AN.
  • Traverse until N > 0 and in each iteration, perform Right Shift operation on it.
  • Also, in each iteration, multiply A with itself and update it.
  • If current LSB is set, then multiply current value of A to ans.
  • Finally, after completing the above steps, print ans.

Below is the implementation of the above approach:

C++




// C++ Program to implement
// the above approach
#include <iostream>
using namespace std;
 
// Function to return a^n
int powerOptimised(int a, int n)
{
 
    // Stores final answer
    int ans = 1;
 
    while (n > 0) {
 
        int last_bit = (n & 1);
 
        // Check if current LSB
        // is set
        if (last_bit) {
            ans = ans * a;
        }
 
        a = a * a;
 
        // Right shift
        n = n >> 1;
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int a = 3, n = 5;
 
    cout << powerOptimised(a, n);
 
    return 0;
}

Java




// Java program to implement
// the above approach
class GFG{
 
// Function to return a^n
static int powerOptimised(int a, int n)
{
 
    // Stores final answer
    int ans = 1;
 
    while (n > 0)
    {
        int last_bit = (n & 1);
 
        // Check if current LSB
        // is set
        if (last_bit > 0)
        {
            ans = ans * a;
        }
         
        a = a * a;
 
        // Right shift
        n = n >> 1;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int a = 3, n = 5;
 
    System.out.print(powerOptimised(a, n));
}
}
 
// This code is contributed by PrinciRaj1992

Python3




# Python3 program to implement
# the above approach
 
# Function to return a^n
def powerOptimised(a, n):
     
    # Stores final answer
    ans = 1
     
    while (n > 0):
        last_bit = (n & 1)
         
        # Check if current LSB
        # is set
        if (last_bit):
            ans = ans * a
        a = a * a
         
        # Right shift
        n = n >> 1
         
    return ans
 
# Driver code
if __name__ == '__main__':
     
    a = 3
    n = 5
     
    print(powerOptimised(a,n))
 
# This code is contributed by virusbuddah_

C#




// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to return a^n
static int powerOptimised(int a, int n)
{
     
    // Stores readonly answer
    int ans = 1;
 
    while (n > 0)
    {
        int last_bit = (n & 1);
 
        // Check if current LSB
        // is set
        if (last_bit > 0)
        {
            ans = ans * a;
        }
        a = a * a;
 
        // Right shift
        n = n >> 1;
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int a = 3, n = 5;
 
    Console.Write(powerOptimised(a, n));
}
}
 
// This code is contributed by Princi Singh

Javascript




<script>
 
// Javascript program to implement
// the above approach
 
// Function to return a^n
function powerOptimised(a, n)
{
   
    // Stores final answer
    let ans = 1;
   
    while (n > 0)
    {
        let last_bit = (n & 1);
   
        // Check if current LSB
        // is set
        if (last_bit > 0)
        {
            ans = ans * a;
        }
           
        a = a * a;
   
        // Right shift
        n = n >> 1;
    }
    return ans;
 
    // Driver Code
         
    let a = 3, n = 5;
   
    document.write(powerOptimised(a, n));
       
</script>

Output: 

243

 

Time Complexity: O(logN) 
Auxiliary Space: O(1)
 


My Personal Notes arrow_drop_up
Last Updated : 31 Oct, 2022
Like Article
Save Article
Similar Reads
Related Tutorials