Skip to content

Instantly share code, notes, and snippets.

@ArduinoDiscordBot
Created January 6, 2021 00:15
Show Gist options
  • Save ArduinoDiscordBot/c4d80a25edfaf42caa4cf72512eb0c22 to your computer and use it in GitHub Desktop.
Save ArduinoDiscordBot/c4d80a25edfaf42caa4cf72512eb0c22 to your computer and use it in GitHub Desktop.
Code by boat#3031 - Wed Jan 06 2021 00:15:36 GMT+0000 (Coordinated Universal Time)

This gist was pasted by the Arduino discord server bot.

This gist was automatically pasted at the request of the code author or one of the discord server helpers. If you have any suggestions or bugs to report, you can do so on our GitHub repository, or in our discord server. This project is run by volunteers so feel free to fork and commit your changes then open a pull request!


⬇️ Pasted Code ⬇️

long exponentMod(long base, int exponent, long modulus){
long numToReturn = 1;
int count = sizeof(exponent) * 8;
int temp;
for(int i = 0; i < count; i++){
temp = (exponent & ( 1 << i )) >> i;
if (temp == 1) {
numToReturn = (numToReturn * squaremoduloMethod(base, (int) pow(2, i), modulus) % modulus);
}
}
return numToReturn;
}
long squaremoduloMethod(long base, int exponent, long modulus ){
//assume that exponent is a square
//this funciton works perfectly
long temp = base;
short count = (log(exponent) / log(2));
for(short i = 0; i < count; i++){
temp = (long)pow(temp, 2) % modulus ;
}
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment