Does anyone have experience with java? If yes, please quickly take a look at my code. I am writing code for modular exponentiation, and I am getting the wrong output :(
String binaryexponent = Long.toBinaryString(exponent); String reverse = new StringBuilder(binaryexponent).reverse().toString(); for (int i = 0; i < reverse.length(); i++) { if (reverse.substring(i, i + 1).equals("1")) { answer *= ((long) Math.pow(base, Math.pow(2, i))) % modulus; System.out.println(Math.pow(2, i)); } } answer %= modulus;
And suppose that all my variables have been declared. When I take out the casting for long on the 8th line, I get a different output. Why does it do that?
I don't understand what you're trying to do really, but here's a method I made not too long back that calculates exponents in modular arithmetic. You can try it out there, I have an example commented ready to run. https://repl.it/EtlR/3
YES AND YES AND YAAAAAAS
Ooh 0_0, thank you so much @kainui lol @Miracrown
Join our real-time social learning platform and learn together with your friends!