Created
November 18, 2014 13:27
Modular Exponentiation which returns -6593454 instead of 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int modexp(int m, int e, int n); | |
int main() | |
{ | |
int p = 3511; | |
printf("%i\n", modexp(2, p-1, p*p)); | |
} | |
int modexp(int m, int e, int n) | |
{ | |
printf("%i\n", m); | |
printf("%i\n", e); | |
printf("%i\n", n); | |
printf("\n"); | |
if(e == 0) | |
{ | |
return 1; | |
} | |
if(e%2 == 1) | |
{ | |
return modexp(m, e-1, n) * m%n; | |
} | |
else | |
{ | |
int modTemp = modexp(m, (int)(e/2), n); | |
modTemp = modTemp * modTemp; | |
return modTemp % n; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment