Created
May 4, 2020 00:25
-
-
Save SumitJainUTD/0108fec0d5e7057ee375751805d6f271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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