solved

Printable View

- 02-14-2013, 09:54 AMwilliamsb14solved
solved

- 02-14-2013, 10:49 AMJosAHRe: Recursion to Iteration exponents Help
You're doing way too much in your implementation; think of the exponent as a binary number; if bit i is one, the result should be multiplied by base^(i+1); e.g. if the exponent is 5, the result should be b^4*b^1. In Java this means:

Code:`public double power(double base, int n) {`

double result= 1;

for (; n > 0; n/= 2, base*= base)

if ((n%2) != 0) result*= base;

return result;

}

Jos