java - Is there a better (more correct) way to calculate modulus of some power than Math.pow? -


consider following simple block of java code:

public static void main(string[] args) {   int = 7;   double exp;   (int i=0; i<71; ++i) {     exp = math.pow(a,i) % 71;     if (exp == 59.0)       system.out.printf("k=%d: %.0f%n", i, exp);   } } 

i trying iterate through of exponents k such a^x=59 mod 71... there should single one, , i'm getting three: k=3, k=23, k=63. wrong, upon verification, k=3 correct answer. there inherent problem in way floating point works (and math.pow), or there other logic problem in code? should using other method calculate a^x (mod n)?

the simplest correct expedient doesn't require writing own method probably

biginteger.valueof(a)     .modpow(biginteger.valueof(i), biginteger.valueof(71))     .intvalue(); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -