Skip to content

Instantly share code, notes, and snippets.

Created November 18, 2014 13:27
Show Gist options
  • Save anonymous/7024ac77b2432a381968 to your computer and use it in GitHub Desktop.
Save anonymous/7024ac77b2432a381968 to your computer and use it in GitHub Desktop.
Modular Exponentiation which returns -6593454 instead of 1
#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