#include #include "bigint.h" #include "power.h" Power::Power () { bitstack = new Stack(NULL); } BigInt *Power::pow (int x, int y) { // Bits of the exponent go on a stack while (y > 1) { bitstack->push(new bool(y%2)); y /= 2; } // Initialize p to the mantissa BigInt *mantissa, *p; p = mantissa = new BigInt(x); // Repeatedly square or square and multiply until // all bits of y are considered while (!bitstack->empty()) { p = p->multiplyBigInt(p); if (*(bool*)bitstack->pop()) p = p->multiplyBigInt(mantissa); } return p; }