Given three numbers x, y and p, compute (xy) % p.
Examples :
Input: x = 2, y = 3, p = 5
Output: 3
Explanation: 2^3 % 5 = 8 % 5 = 3.
Input: x = 2, y = 5, p = 13
Output: 6
Explanation: 2^5 % 13 = 32 % 13 = 6.
We have discussed recursive and iterative solutions for power.
Below is discussed iterative solution.
C++14
#include <iostream>
using namespace std;
int power(int x, int y, int p)
{
int res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res % p;
}
int main()
{
int x = 2;
int y = 5;
int p = 13;
cout << "Power is " << power(x, y, p);
return 0;
}
|
Java
class GFG {
static int power(int x, int y, int p)
{
int res = 1;
while (y > 0) {
if ((y & 1) != 0)
res = res * x;
y = y >> 1;
x = x * x;
}
return res % p;
}
public static void main(String[] args)
{
int x = 2;
int y = 5;
int p = 13;
int mod = power(x, y, p);
System.out.print("Power is " + mod);
}
}
|
Python3
def power(x, y, p):
res = 1
while (y > 0):
if ((y & 1) != 0):
res = res * x
y = y >> 1
x = x * x
return res % p
x = 2
y = 5
p = 13
print("Power is ", power(x, y, p))
|
Javascript
<script>
function power(x, y, p)
{
let res = 1;
while (y > 0)
{
if (y & 1)
res = res*x;
y = y>>1;
x = x*x;
}
return res % p;
}
let x = 2;
let y = 5;
let p = 13;
document.write("Power is " + power(x, y, p));
</script>
|
C#
using System;
class GFG {
static int power(int x, int y, int p)
{
int res = 1;
while (y > 0) {
if ((y & 1) != 0)
res = res * x;
y = y >> 1;
x = x * x;
}
return res % p;
}
static public void Main()
{
int x = 2;
int y = 5;
int p = 13;
Console.Write("Power is " + power(x, y, p));
}
}
|
Time Complexity: O(log2y), where y represents the value of the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Efficient Approach:
The problem with the above solutions is, overflow may occur for large values of n or x. Therefore, power is generally evaluated under the modulo of a large number.
Below is the fundamental modular property that is used for efficiently computing power under modular arithmetic.
(ab) mod p = ( (a mod p) (b mod p) ) mod p
For example a = 50, b = 100, p = 13
50 mod 13 = 11
100 mod 13 = 9
(50 * 100) mod 13 = ( (50 mod 13) * (100 mod 13) ) mod 13
or (5000) mod 13 = ( 11 * 9 ) mod 13
or 8 = 8
Below is the implementation based on the above property.
C++14
#include <iostream>
using namespace std;
int power(long long x, unsigned int y, int p)
{
int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
int main()
{
int x = 2;
int y = 5;
int p = 13;
cout << "Power is " << power(x, y, p);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static void main(String[] args)
{
int x = 2;
int y = 5;
int p = 13;
System.out.print("Power is " + power(x, y, p));
}
}
|
Python3
def power(x, y, p) :
res = 1
x = x % p
if (x == 0) :
return 0
while (y > 0) :
if ((y & 1) == 1) :
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
x = 2; y = 5; p = 13
print("Power is ", power(x, y, p))
|
C#
using System;
public class GFG
{
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static public void Main ()
{
int x = 2;
int y = 5;
int p = 13;
Console.Write("Power is " + power(x, y, p));
}
}
|
PHP
<?php
function power($x, $y, $p)
{
$res = 1;
$x = $x % $p;
if ($x == 0)
return 0;
while ($y > 0)
{
if ($y & 1)
$res = ($res * $x) % $p;
$y = $y >> 1;
$x = ($x * $x) % $p;
}
return $res;
}
$x = 2;
$y = 5;
$p = 13;
echo "Power is ", power($x, $y, $p);
?>
|
Javascript
function power(x, y, p)
{
let res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
let x = 2;
let y = 5;
let p = 13;
document.write("Power is " + power(x, y, p));
|
Time Complexity: O(Log y), where y represents the value of the given input.
Auxiliary Space: O(1), as we are not using any extra space.
Modular exponentiation (Recursive)
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Jun, 2022
Like Article
Save Article