Fast Exponentiation using Bit Manipulation
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.
C++
#include <iostream>
using namespace std;
long long findPower( int a, int n) {
long long result = 1;
for ( int i = 0; i < n; ++i) {
result *= a;
}
return result;
}
int main() {
int a = 3;
int n = 5;
long long result = findPower(a, n);
cout<<result<<endl;
return 0;
}
|
Java
public class Main {
static long findPower( int a, int n) {
long result = 1 ;
for ( int i = 0 ; i < n; ++i) {
result *= a;
}
return result;
}
public static void main(String[] args) {
int a = 3 ;
int n = 5 ;
long result = findPower(a, n);
System.out.println(result);
}
}
|
C#
using System;
public class PowerCalculation
{
public static long FindPower( int a, int n)
{
long result = 1;
for ( int i = 0; i < n; ++i)
{
result *= a;
}
return result;
}
public static void Main( string [] args)
{
int a = 3;
int n = 5;
long result = FindPower(a, n);
Console.WriteLine(result);
}
}
|
Javascript
function findPower(a, n) {
let result = 1;
for (let i = 0; i < n; i++) {
result *= a;
}
return result;
}
const a = 3;
const n = 5;
const result = findPower(a, n);
console.log(result);
|
Python3
def findPower(a, n):
result = 1
for i in range (n):
result * = a
return result
a = 3
n = 5
result = findPower(a, n)
print (result)
|
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++
#include <iostream>
using namespace std;
int powerOptimised( int a, int n)
{
int ans = 1;
while (n > 0) {
int last_bit = (n & 1);
if (last_bit) {
ans = ans * a;
}
a = a * a;
n = n >> 1;
}
return ans;
}
int main()
{
int a = 3, n = 5;
cout << powerOptimised(a, n);
return 0;
}
|
Java
class GFG{
static int powerOptimised( int a, int n)
{
int ans = 1 ;
while (n > 0 )
{
int last_bit = (n & 1 );
if (last_bit > 0 )
{
ans = ans * a;
}
a = a * a;
n = n >> 1 ;
}
return ans;
}
public static void main(String[] args)
{
int a = 3 , n = 5 ;
System.out.print(powerOptimised(a, n));
}
}
|
C#
using System;
class GFG{
static int powerOptimised( int a, int n)
{
int ans = 1;
while (n > 0)
{
int last_bit = (n & 1);
if (last_bit > 0)
{
ans = ans * a;
}
a = a * a;
n = n >> 1;
}
return ans;
}
public static void Main(String[] args)
{
int a = 3, n = 5;
Console.Write(powerOptimised(a, n));
}
}
|
Javascript
<script>
function powerOptimised(a, n)
{
let ans = 1;
while (n > 0)
{
let last_bit = (n & 1);
if (last_bit > 0)
{
ans = ans * a;
}
a = a * a;
n = n >> 1;
}
return ans;
}
let a = 3, n = 5;
document.write(powerOptimised(a, n));
</script>
|
Python3
def powerOptimised(a, n):
ans = 1
while (n > 0 ):
last_bit = (n & 1 )
if (last_bit):
ans = ans * a
a = a * a
n = n >> 1
return ans
if __name__ = = '__main__' :
a = 3
n = 5
print (powerOptimised(a,n))
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Another efficient approach : Recursive exponentiation
Recursive exponentiation is a method used to efficiently compute AN, where A & N are integers. It leverages recursion to break down the problem into smaller subproblems. Follow the steps below :
- If N = 0, the result is always 1 because any non zero number raised to the power of 0 is 1.
- If N is even, we can compute AN as (AN/2)2 . This is done by recursively computing AN/2 & squaring the result.
- If N is odd, we compute AN as A * AN – 1 . Again, we recursively compute AN -1 & multiply the result by A.
Below is the implementation of the above approach :
Python
def recursive_exponentiation(A, N):
if N = = 0 :
return 1
elif N % 2 = = 0 :
temp = recursive_exponentiation(A, N / / 2 )
return temp * temp
else :
return A * recursive_exponentiation(A, N - 1 )
print (recursive_exponentiation( 3 , 5 ))
|
Complexity Analysis :
Time Complexity: O(logN)
Auxiliary Space: O(1)
Last Updated :
11 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...