Skip to content

Instantly share code, notes, and snippets.

@SumitJainUTD
Created May 4, 2020 00:25
Show Gist options
  • Save SumitJainUTD/0108fec0d5e7057ee375751805d6f271 to your computer and use it in GitHub Desktop.
Save SumitJainUTD/0108fec0d5e7057ee375751805d6f271 to your computer and use it in GitHub Desktop.
public class CalculateModOfPow {
public static void calculateModOfPow(int x, int y, int z){
int result = calculate(x, y, z);
System.out.println("("+x+" ^ "+y+") % "+z+" = "+result);
}
public static int calculate(int n, int y, int z){
if(y==0)
return 1;
if(y%2!=0)
return calculate(n, y/2, z)*calculate(n, y/2, z)*n%z;
else
return calculate(n, y/2, z)*calculate(n, y/2, z)%z;
}
public static void main(String[] args) {
int n = 2;
int pow = 8;
int z = 7;
calculateModOfPow(n, pow, z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment