I need to calculate 0 ^ j, 1 ^ j, ..., k ^ j for some very large k and j (both orders of several millions). I use GMP to handle large integers (yes, I need integers as I need full precision). Now, I wonder when I try to calculate n ^ j, is there no way to speed up the calculation of (n + 1) ^ j instead of starting from scratch?
Here is the algorithm I'm using now to calculate power:
mpz_class pow(unsigned long int b, unsigned long int e)
{
mpz_class res = 1;
mpz_class m = b;
while(e)
{
if(e & 1)
{
res *= m;
}
e >>= 1;
m *= m;
}
return res;
}
As you can see, every time I start from scratch and it takes a lot of time.
source
share