Exponential Squaring (Fast Modulo Multiplication)
Last Updated :
03 Oct, 2023
Given two numbers base and exp, we need to compute baseexp under Modulo 10^9+7
Examples:
Input : base = 2, exp = 2
Output : 4
Input : base = 5, exp = 100000
Output : 754573817
In competitions, for calculating large powers of a number we are given a modulus value(a large prime number) because as the values of is being calculated it can get very large so instead we have to calculate (%modulus value.) We can use the modulus in our naive way by using modulus on all the intermediate steps and take modulus at the end, but in competitions it will definitely show TLE. So, what we can do. The answer is we can try exponentiation by squaring which is a fast method for calculating exponentiation of a number. Here we will be discussing two most common/important methods:
- Basic Method(Binary Exponentiation)
- -ary method.
Binary Exponentiation
As described in this article we will be using following formula to recursively calculate (%modulus value):
C++
#include<iostream>
using namespace std;
#define N 1000000007 // prime modulo value
long int exponentiation( long int base,
long int exp )
{
if ( exp == 0)
return 1;
if ( exp == 1)
return base % N;
long int t = exponentiation(base, exp / 2);
t = (t * t) % N;
if ( exp % 2 == 0)
return t;
else
return ((base % N) * t) % N;
}
int main()
{
long int base = 5;
long int exp = 100000;
long int modulo = exponentiation(base, exp );
cout << modulo << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class exp_sq {
static long N = 1000000007L;
public static void main(String[] args)
{
long base = 5 ;
long exp = 100000 ;
long modulo = exponentiation(base, exp);
System.out.println(modulo);
}
static long exponentiation( long base, long exp)
{
if (exp == 0 )
return 1 ;
if (exp == 1 )
return base % N;
long t = exponentiation(base, exp / 2 );
t = (t * t) % N;
if (exp % 2 == 0 )
return t;
else
return ((base % N) * t) % N;
}
}
|
Python3
N = 1000000007 ;
def exponentiation(bas, exp):
if (exp = = 0 ):
return 1 ;
if (exp = = 1 ):
return bas % N;
t = exponentiation(bas, int (exp / 2 ));
t = (t * t) % N;
if (exp % 2 = = 0 ):
return t;
else :
return ((bas % N) * t) % N;
bas = 5 ;
exp = 100000 ;
modulo = exponentiation(bas, exp);
print (modulo);
|
C#
using System;
class GFG {
static long N = 1000000007L;
public static void Main()
{
long bas = 5;
long exp = 100000;
long modulo = exponentiation(bas, exp);
Console.Write(modulo);
}
static long exponentiation( long bas, long exp)
{
if (exp == 0)
return 1;
if (exp == 1)
return bas % N;
long t = exponentiation(bas, exp / 2);
t = (t * t) % N;
if (exp % 2 == 0)
return t;
else
return ((bas % N) * t) % N;
}
}
|
Javascript
<script>
let N = 1000000007;
function exponentiation(base, exp){
if (exp == 0){
return 1;
}
if (exp == 1){
return (base % N);
}
let t = exponentiation(base,exp/2);
t = ((t * t) % N);
console.log(t);
if (exp % 2 == 0){
return t;
}
else {
return ((base % N) * t) % N;
}
}
let base = 5;
let exp = 100000;
let modulo = exponentiation(base, exp);
document.write(modulo);
document.write(base);
</script>
|
PHP
<?php
$N = 1000000007;
function exponentiation( $bas , $exp )
{
global $N ;
if ( $exp == 0)
return 1;
if ( $exp == 1)
return $bas % $N ;
$t = exponentiation( $bas ,
$exp / 2);
$t = ( $t * $t ) % $N ;
if ( $exp % 2 == 0)
return $t ;
else
return (( $bas % $N ) *
$t ) % $N ;
}
$bas = 5;
$exp = 100000;
$modulo = exponentiation( $bas , $exp );
echo ( $modulo );
?>
|
Output :
754573817
Time Complexity: O(log exp) since the binary exponentiation algorithm divides the exponent by 2 at each recursive call, resulting in a logarithmic number of recursive calls.
Space Complexity: O(log exp)
-ary method:
In this algorithm we will be expanding the exponent in base (k>=1), which is somehow similar to above method except we are not using recursion this method uses comparatively less memory and time.
C++
#include<bits/stdc++.h>
using namespace std;
#define N 1000000007L; // prime modulo value
long exponentiation( long base, long exp )
{
long t = 1L;
while ( exp > 0)
{
if ( exp % 2 != 0)
t = (t * base) % N;
base = (base * base) % N;
exp /= 2;
}
return t % N;
}
int main()
{
long base = 5;
long exp = 100000;
long modulo = exponentiation(base, exp );
cout << (modulo);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class exp_sq {
static long N = 1000000007L;
public static void main(String[] args)
{
long base = 5 ;
long exp = 100000 ;
long modulo = exponentiation(base, exp);
System.out.println(modulo);
}
static long exponentiation( long base, long exp)
{
long t = 1L;
while (exp > 0 ) {
if (exp % 2 != 0 )
t = (t * base) % N;
base = (base * base) % N;
exp /= 2 ;
}
return t % N;
}
}
|
Python3
N = 1000000007 ;
def exponentiation(bas, exp):
t = 1 ;
while (exp > 0 ):
if (exp % 2 ! = 0 ):
t = (t * bas) % N;
bas = (bas * bas) % N;
exp = int (exp / 2 );
return t % N;
bas = 5 ;
exp = 100000 ;
modulo = exponentiation(bas,exp);
print (modulo);
|
C#
using System;
class GFG
{
static long N = 1000000007L;
static long exponentiation( long bas,
long exp)
{
long t = 1L;
while (exp > 0)
{
if (exp % 2 != 0)
t = (t * bas) % N;
bas = (bas * bas) % N;
exp /= 2;
}
return t % N;
}
public static void Main ()
{
long bas = 5;
long exp = 100000;
long modulo = exponentiation(bas,
exp);
Console.WriteLine(modulo);
}
}
|
Javascript
let N = 1000000007n;
function exponentiation(bas, exp)
{
let t = 1n;
while (exp > 0n)
{
if (exp % 2n != 0n)
t = (t * bas) % N;
bas = (bas * bas) % N;
exp >>= 1n;
}
return t % N;
}
let bas = 5n;
let exp = 100000n;
let modulo = exponentiation(bas,exp);
console.log(Number(modulo));
|
PHP
<?php
$N = 1000000007;
function exponentiation( $bas ,
$exp )
{
global $N ;
$t = 1;
while ( $exp > 0)
{
if ( $exp % 2 != 0)
$t = ( $t * $bas ) % $N ;
$bas = ( $bas * $bas ) % $N ;
$exp = (int) $exp / 2;
}
return $t % $N ;
}
$bas = 5;
$exp = 100000;
$modulo = exponentiation( $bas ,
$exp );
echo ( $modulo );
?>
|
Output :
754573817
Time Complexity: O(log exp)
Space Complexity: O(1)
Bit-Manipulation Method
The basic idea behind the algorithm is to use the binary representation of the exponent to compute the power in a faster way.
Specifically, if we can represent the exponent as a sum of powers of 2, then we can use the fact that x^(a+b) = x^a * x^b to compute the power.
Approach :
The steps of the algorithm are as follows :
1. Initialize a result variable to 1, and a base variable to the given base value.
2. Convert the exponent to binary format.
3. Iterate over the bits of the binary representation of the exponent, from right to left.
4. For each bit, square the current value of the base.
5. If the current bit is 1, multiply the result variable by the current value of the base.
6. Divide the exponent by 2, discarding the remainder.
7. Continue the iteration until all bits of the exponent have been processed.
8. Return the result variable modulo the given modulus value.
C++
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007LL;
long long squareMultiply( long long base, long long exp ) {
long long b = 1LL;
long long A = base % mod;
if ( exp & 1LL) {
b = base % mod;
}
exp >>= 1LL;
while ( exp > 0) {
A = (A * A) % mod;
if ( exp & 1LL) {
b = (A * b) % mod;
}
exp >>= 1LL;
}
return b % mod;
}
int main() {
long long base = 5LL;
long long exp = 100000LL;
long long modulo = squareMultiply(base, exp );
cout << modulo << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class exp_sq {
static long mod = 1000000007L;
public static void main(String[] args)
{
long base = 5 ;
long exp = 100000 ;
long modulo = squareMultiply(base, exp);
System.out.println(modulo);
}
static long squareMultiply( long base, long exp)
{
long b = 1 ;
long A = base;
if ((exp & 1 ) == 1 ){
b = base % mod;
}
exp = (exp >> 1 );
while (exp > 0 ){
A = (A * A) % mod;
if ((exp & 1 ) == 1 ){
b = (A * b) % mod;
}
exp = (exp >> 1 );
}
return b % mod;
}
}
|
Python3
mod = 1000000007
def square_multiply(base, exp):
b = 1
A = base % mod
if exp & 1 :
b = base % mod
exp >> = 1
while exp > 0 :
A = (A * A) % mod
if exp & 1 :
b = (A * b) % mod
exp >> = 1
return b % mod
base = 5
exp = 100000
modulo = square_multiply(base, exp)
print (modulo)
|
C#
using System;
public class Program {
const long mod = 1000000007L;
static long squareMultiply( long baseNum, long exponent)
{
long b = 1L;
long A = baseNum % mod;
if ((exponent & 1L) == 1L) {
b = baseNum % mod;
}
exponent >>= 1;
while (exponent > 0) {
A = (A * A) % mod;
if ((exponent & 1L) == 1L) {
b = (A * b) % mod;
}
exponent >>= 1;
}
return b % mod;
}
static public void Main()
{
long baseNum = 5L;
long exponent = 100000L;
long modulo = squareMultiply(baseNum, exponent);
Console.WriteLine(modulo);
}
}
|
Javascript
const mod = 1000000007n;
function squareMultiply(base, exp) {
let b = 1n;
let A = base % mod;
if (exp & 1n) {
b = base % mod;
}
exp >>= 1n;
while (exp > 0n) {
A = (A * A) % mod;
if (exp & 1n) {
b = (A * b) % mod;
}
exp >>= 1n;
}
return b % mod;
}
const base = 5n;
const exp = 100000n;
const modulo = squareMultiply(base, exp);
console.log(modulo.toString());
|
Time Complexity — O( log(exp) )
Space Complexity — O(1)
Applications: Besides fast calculation of this method have several other advantages, like it is used in cryptography, in calculating Matrix Exponentiation et cetera.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...