Advertisement
cd62131

modpow in c

Aug 9th, 2017
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.32 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // x ^ y % n
  4. int modpow(int x, int y, int n) {
  5.   int ret = 1;
  6.   for (; y; x = x * x % n, y >>= 1) {
  7.     if (y & 1) {
  8.       ret = ret * x % n;
  9.     }
  10.   }
  11.   return ret;
  12. }
  13.  
  14. int main(void) {
  15.   printf("2 ^ 10 %% 7 = %d\n", modpow(2, 10, 7));
  16.   printf("2 ^ 20 %% 7 = %d\n", modpow(2, 20, 7));
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement