C/C++ Large number calculation
I'm trying to compute the following number in a C program : result = (3 * pow(2,500000000) - 2 ) % 1000000000 The power of 2 is way to large to be handled properly => I'm under the impression I could split the calculation in many steps using the modulo to reduce the result size. Does someone has a strategy for doing so ? Any other idea ? Thanx in advance Manu The simplest method is exponentiation by repeated squaring reducing by the modulus in each step. unsigned long long mod_pow(unsigned long long base, unsigned long long exponent, unsigned long long modulus) { if (exponent == 0) return 1;