Modular exponentiation (Recursive)

Given three numbers a, b and c, we need to find (ab) % c

Now why do “% c” after exponentiation, because ab will be really large even for relatively small values of a, b and that is a problem because the data type of the language that we try to code the problem, will most probably not let us store such a large number.

Examples:



Input : a = 2312 b = 3434 c = 6789
Output : 6343

Input : a = -3 b = 5 c = 89 
Output : 24

The idea is based on below properties.

Property 1:
(m * n) % p has a very interesting property:
(m * n) % p =((m % p) * (n % p)) % p

Property 2:
if b is even:
(a ^ b) % c = ((a ^ b/2) * (a ^ b/2))%c ? this suggests divide and conquer
if b is odd:
(a ^ b) % c = (a * (a ^( b-1))%c

Property 3:
If we have to return the mod of a negative number x whose absolute value is less than y:
then (x + y) % y will do the trick

Note:
Also as the product of (a ^ b/2) * (a ^ b/2) and a * (a ^( b-1) may cause overflow, hence we must be careful about those scenarios

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive C++ program to compute modular power 
#include <bits/stdc++.h> 
using namespace std;
  
int exponentMod(int A, int B, int C) 
    // Base cases 
    if (A == 0) 
        return 0; 
    if (B == 0) 
        return 1; 
  
    // If B is even 
    long y; 
    if (B % 2 == 0) { 
        y = exponentMod(A, B / 2, C); 
        y = (y * y) % C; 
    
  
    // If B is odd 
    else
        y = A % C; 
        y = (y * exponentMod(A, B - 1, C) % C) % C; 
    
  
    return (int)((y + C) % C); 
  
// Driver code 
int main() 
    int A = 2, B = 5, C = 13; 
    cout << "Power is " << exponentMod(A, B, C); 
    return 0; 
  
// This code is contributed by SHUBHAMSINGH10

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive C program to compute modular power 
#include <stdio.h> 
  
int exponentMod(int A, int B, int C)
{
    // Base cases
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
  
    // If B is even
    long y;
    if (B % 2 == 0) {
        y = exponentMod(A, B / 2, C);
        y = (y * y) % C;
    }
  
    // If B is odd
    else {
        y = A % C;
        y = (y * exponentMod(A, B - 1, C) % C) % C;
    }
  
    return (int)((y + C) % C);
}
  
// Driver program to test above functions 
int main() 
   int A = 2, B = 5, C = 13;
   printf("Power is %d", exponentMod(A, B, C)); 
   return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive Java program 
// to compute modular power 
import java.io.*; 
  
class GFG 
static int exponentMod(int A, 
                       int B, int C) 
          
    // Base cases 
    if (A == 0
        return 0
    if (B == 0
        return 1
      
    // If B is even 
    long y; 
    if (B % 2 == 0)
    
        y = exponentMod(A, B / 2, C); 
        y = (y * y) % C; 
    
      
    // If B is odd 
    else 
    
        y = A % C; 
        y = (y * exponentMod(A, B - 1
                             C) % C) % C; 
    
      
    return (int)((y + C) % C); 
  
// Driver Code
public static void main(String args[]) 
    int A = 2, B = 5, C = 13
    System.out.println("Power is "
                        exponentMod(A, B, C)); 
  
// This code is contributed 
// by Swetank Modi. 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Recursive Python program 
# to compute modular power 
def exponentMod(A, B, C):
      
    # Base Cases
    if (A == 0):
        return 0
    if (B == 0):
        return 1
      
    # If B is Even
    y = 0
    if (B % 2 == 0):
        y = exponentMod(A, B / 2, C)
        y = (y * y) % C
      
    # If B is Odd
    else:
        y = A % C
        y = (y * exponentMod(A, B - 1
                             C) % C) % C
    return ((y + C) % C)
  
# Driver Code
A = 2
B = 5
C = 13
print("Power is", exponentMod(A, B, C))
      
# This code is contributed 
# by Swetank Modi. 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive C# program 
// to compute modular power 
class GFG 
static int exponentMod(int A, int B, int C) 
          
    // Base cases 
    if (A == 0) 
        return 0; 
    if (B == 0) 
        return 1; 
      
    // If B is even 
    long y; 
    if (B % 2 == 0)
    
        y = exponentMod(A, B / 2, C); 
        y = (y * y) % C; 
    
      
    // If B is odd 
    else
    
        y = A % C; 
        y = (y * exponentMod(A, B - 1, 
                             C) % C) % C; 
    
      
    return (int)((y + C) % C); 
  
// Driver Code
public static void Main() 
    int A = 2, B = 5, C = 13; 
    System.Console.WriteLine("Power is "
                    exponentMod(A, B, C)); 
  
// This code is contributed 
// by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Recursive PHP program to 
// compute modular power 
function exponentMod($A, $B, $C
    // Base cases 
    if ($A == 0) 
        return 0; 
    if ($B == 0) 
        return 1; 
      
    // If B is even 
    if ($B % 2 == 0) 
    
        $y = exponentMod($A, $B / 2, $C); 
        $y = ($y * $y) % $C
    
      
    // If B is odd 
    else
    
        $y = $A % $C
        $y = ($y * exponentMod($A, $B - 1, 
                               $C) % $C) % $C
    
      
    return (($y + $C) % $C); 
  
  
// Driver Code
$A = 2;
$B = 5;
$C = 13; 
echo "Power is " . exponentMod($A, $B, $C); 
  
// This code is contributed 
// by Swetank Modi. 
?>

chevron_right


Output:

Power is 6


Iterative modular exponentiation.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.