finding modulus of huge numbers in java -
this question has answer here:
- how calculate modulus of large numbers? 10 answers
for(i=0; i<n+1; i++) { y=y+(a[i]*(int)math.pow(j,i)); } int r=y/786433; s[k]=y-(r*786433); k++;
now in code j
value can 786432
. when try modulus of number (1+2*(786432)^2+3*(786432)^3)%786433
-521562
not correct using modulus operator before got same answer approach getting same answer. in approach modulus of number stored in array s[k]
. can help?
if use math.pow using double types. convert int. rounding can happen , truncating if values big.
to solve problem need use biginteger
:
immutable arbitrary-precision integers
in particular method mod:
returns biginteger value (this mod m). method differs remainder in returns non-negative biginteger.
Comments
Post a Comment