1
我想實現模冪,但我不能得到正確的答案:模冪(算法給出了錯誤的答案)
公共靜態的BigInteger modPow(BigInteger的B,BigInteger的E,BigInteger的M)
{//計算模冪和返回的BigInteger類
BigInteger x= new BigInteger("1"); //The default value of x
BigInteger power ;
power=b.mod(m);
String t =e.toString(2); //convert the power to string of binary
String reverse = new StringBuffer(t).reverse().toString();
for (int i=0;i<reverse.length();i++) { //this loop to go over the string char by char by reverse
if(reverse.charAt(i)=='1') { //the start of if statement when the char is 1
x=x.multiply(power);
x=x.mod(m);
power=power.multiply(power);
power=power.mod(m);
} //the end of if statement
}//the end of for loop
return x;
} //the end of the method modPow
非常感謝 現在是OK – user2835815
這可能是我最喜歡的StackOverflow的評論不斷。 – erickson
我已經試過許多具有512位和它沒有工作:( – user2835815