Skip to content

Instantly share code, notes, and snippets.

@yinyanghu
Last active August 29, 2015 14:23
Show Gist options
  • Save yinyanghu/a61708f067f59c9cf713 to your computer and use it in GitHub Desktop.
Save yinyanghu/a61708f067f59c9cf713 to your computer and use it in GitHub Desktop.
repeated squaring to compute x^y mod p
// return x^y mod p
int repeated_squaring(int x, int y, int p) {
if (y <= 0) return 1;
int ret = 1;
for (int base = x; y; base = (base * base) % p, y >>= 1)
if ((y & 1) == 1) ret = (ret * base) % p;
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment