Iterative Implementation of Binary Exponentiation

C++
#include <bits/stdc++.h>
using namespace std;

long long power(long long a, long long b) {
    long long result = 1;
    while(b) {
        if (b & 1) 
        result = result * a;
        a = a * a;
        b >>= 1;
    }
    return result;
}

int main() {
    cout<<power(2, 12)<<"\n";
    return 0;
}
Java
public class Main {
    public static long power(long a, long b) {
        long result = 1;
        while (b > 0) {
            if ((b & 1) == 1) {
                result *= a;
            }
            a *= a;
            b >>= 1;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(power(2, 12));
    }
}
Python
def power(a, b):
    result = 1
    while b:
        if b & 1:
            result = result * a
        a = a * a
        b >>= 1
    return result

if __name__ == "__main__":
    print(power(2, 12))
C#
using System;

class Program {
    // Function to calculate power
    static long Power(long a, long b)
    {
        long result = 1;
        while (b > 0) {
            if ((b & 1) == 1)
                result *= a;
            a *= a;
            b >>= 1;
        }
        return result;
    }

    static void Main() { Console.WriteLine(Power(2, 12)); }
}
Javascript
// Function to calculate the power of a number
function power(a, b) {
    let result = 1;
    while (b > 0) {
        // If b is odd, multiply result by a
        if (b & 1) {
            result *= a;
        }
        // Square a
        a *= a;
        // Divide b by 2 using bitwise right shift
        b >>= 1;
    }
    return result;
}

// Main function
function main() {
    // Output the result of power(2, 12)
    console.log(power(2, 12));
}

// Call the main function
main();

Output
4096

Even though, both the iterative and recursive approach have the same time complexity, the iterative approach is still faster because there are no overheads for recursive calls.

Binary Exponentiation for Competitive Programming

In competitive programming, we often need to do a lot of big number calculations fast. Binary exponentiation is like a super shortcut for doing powers and can make programs faster. This article will show you how to use this powerful trick to enhance your coding skills.

Table of Content

  • What is Binary Exponentiation?
  • Why to Use Binary Exponentiation?
  • Idea Behind Binary Exponentiation
  • Recursive Implementation of Binary Exponentiation
  • Iterative Implementation of Binary Exponentiation
  • Use Cases of Binary Exponentiation in Competitive Programming
  • Practice Problems on Binary Exponentiation for Competitive Programming

Similar Reads

What is Binary Exponentiation?

...

Why to Use Binary Exponentiation?

Binary Exponentiation or Exponentiation by squaring is the process of calculating a number raised to the power another number (AB) in Logarithmic time of the exponent or power, which speeds up the execution time of the program....

Idea Behind Binary Exponentiation:

Whenever we need to calculate (AB), we can simple calculate the result by taking the result as 1 and multiplying A for exactly B times. The time complexity for this approach is O(B) and will fail when values of B in order of 108 or greater. This is when we can use Binary exponentiation because it can calculate the result in O(log(B)) time complexity, so we can easily calculate the results for larger values of B in order of 1018 or less....

Recursive Implementation of Binary Exponentiation:

When we are calculating (AB), we can have 3 possible positive values of B:...

Iterative Implementation of Binary Exponentiation:

C++ #include using namespace std; long long power(long long A, long long B) { if (B == 0) return 1; long long res = power(A, B / 2); if (B % 2) return res * res * A; else return res * res; } int main() { cout << power(2, 12) << "\n"; return 0; } Java import java.io.*; class GFG { static long power(long A, long B) { if (B == 0) return 1; long res = power(A, B / 2); if (B % 2 == 1) return res * res * A; else return res * res; } public static void main(String[] args) { System.out.println(power(2, 12)); } } //This code is contributed by Rohit Singh Python def power(A, B): if B == 0: return 1 res = power(A, B // 2) if B % 2: return res * res * A else: return res * res if __name__ == "__main__": print(power(2, 12)) C# using System; class Program { // Recursive function to calculate the power of a number (A^B) static long Power(long A, long B) { // Base case: A^0 is always 1 if (B == 0) return 1; // Recursive calculation of A^(B/2) long res = Power(A, B / 2); // Multiply the result by itself res *= res; // If B is odd, multiply by A one more time if (B % 2 != 0) res *= A; return res; } static void Main() { // Example: Calculate and print the result of 2^12 Console.WriteLine(Power(2, 12)); } } Javascript // Function to calculate the power of a number A raised to the power of B function power(A, B) { if (B === 0) { return 1; } let res = power(A, Math.floor(B / 2)); if (B % 2 === 1) { return res * res * A; } else { return res * res; } } // Driver code console.log(power(2, 12));...

Use Cases of Binary Exponentiation in Competitive Programming:

C++ #include using namespace std; long long power(long long a, long long b) { long long result = 1; while(b) { if (b & 1) result = result * a; a = a * a; b >>= 1; } return result; } int main() { cout< 0) { if ((b & 1) == 1) { result *= a; } a *= a; b >>= 1; } return result; } public static void main(String[] args) { System.out.println(power(2, 12)); } } Python def power(a, b): result = 1 while b: if b & 1: result = result * a a = a * a b >>= 1 return result if __name__ == "__main__": print(power(2, 12)) C# using System; class Program { // Function to calculate power static long Power(long a, long b) { long result = 1; while (b > 0) { if ((b & 1) == 1) result *= a; a *= a; b >>= 1; } return result; } static void Main() { Console.WriteLine(Power(2, 12)); } } Javascript // Function to calculate the power of a number function power(a, b) { let result = 1; while (b > 0) { // If b is odd, multiply result by a if (b & 1) { result *= a; } // Square a a *= a; // Divide b by 2 using bitwise right shift b >>= 1; } return result; } // Main function function main() { // Output the result of power(2, 12) console.log(power(2, 12)); } // Call the main function main();...

Practice Problems on Binary Exponentiation for Competitive Programming:

1. Fast Computation of Nth Fibonacci Number:...