Exponential Squaring ( Fast Modulo Multiplication ) | C ++ program to compute exponential value using ( 2 ^ k ) - ary method . ; # define N 1000000007L ; prime modulo value ; for cases where exponent is not an even value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; } int main ( ) { long base = 5 ; long exp = 100000 ; long modulo = exponentiation ( base , exp ) ; cout << ( modulo ) ; return 0 ; }