숫자의 4 승수를 얻고 싶습니다. 여기에 내 코드 : 그것은 잘 작동하지만 문자열로 변환이 불필요한 것, 그리고 %
연산자를 사용하는 a.remainder(b)
보다 더 간결 것Integer.toString()을 우회하는 BigInteger의 조작
static int testMod(int a, int mod) {
/* //This looks clear
BigInteger a4 = a;
return (a4.pow(4))%mod;
*/
//This works
String a2String = Integer.toString(a);
String mod2String = Integer.toString(mod);
BigInteger a4 = new BigInteger(a2String);
BigInteger modBigInt = new BigInteger(mod2String);
a4 = a4.pow(4);
return a4.remainder(modBigInt).intValue();
}
. 더 명확하게 재 작성하는 것이 가능합니까?
#remainder
의는 "4"전원 "네번째"또는이어야한다. – mbomb007https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#valueOf-long- –
a4가 long보다 클 수 있으므로 잠재적으로 정밀도를 잃어 버리고 잘못된 값을 반환합니다. modulus 연산을 수행하기 위해 a4를 int로 변환하면 대답하십시오. – user3745362