Skip to content

Instantly share code, notes, and snippets.

@sujimodern
Last active July 7, 2019 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sujimodern/295912c78f7f287ec1edb1624406bdf2 to your computer and use it in GitHub Desktop.
Save sujimodern/295912c78f7f287ec1edb1624406bdf2 to your computer and use it in GitHub Desktop.
Fermat's little theorem
#include <stdio.h>
int power_mod(int b, int e, int m) {
int r = 1;
for (int i = 0; i < e; ++i) {
r = (r * b) % m;
}
return r;
}
int fermat(int a, int p) {
return power_mod(a, p - 1, p);
}
int main() {
int a = 8;
int p = 11;
int r = fermat(a, p);
printf("%d\n", r);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment