Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Oct 7, 2014
1 parent 52cd1a1 commit 92edcff
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/src/BinaryPow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class BinaryPow {

public static int pow(int x, int n, int mod) {
int res = 1;
for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) {
if ((n & 1) != 0) {
res = (int) (res * p % mod);
}
}
return res;
}

// usage example
public static void main(String[] args) {
System.out.println(8 == pow(2, 3, 100));
}
}

0 comments on commit 92edcff

Please sign in to comment.