I am currently working on a recursive Modular Exponentiation class in Java and I found a way to do it in Python. I worked with Python before Java, so I am not quite sure how to do it in Java.
My Java code is written like this:
public class ModPower { public static int modPower(int x, int p, int m) { x %= m; int ret; if (p == 0) { ret = 1; } else if (p % 2 == 0) // if even { ret = x * modPower(x, p-1, m); } else // if odd { ret = modPower(x, (p/2), m); ret *= ret; } return ret % m; } }
I am getting correct values if I run the python code, but incorrect ones when I run the Java code. I've been reworking it for hours and can't figure out what to do! Not to mention I can't really debug a recursive class. Please help!