Bloomberg Interview Question

Implement a function to raise a number to an exponent using your language of choice.

Interview Answers

Anonymous

Feb 17, 2015

int raisingTo(int base, unsigned int exponent) { if (exponent == 0) return 1; else return base * raisingTo(base, exponent - 1); }

1

Anonymous

Feb 13, 2015

C++: #include double raise(double nm, double ex) { return pow(nm, ex); } The question didn't say that you can't use pow() so ... done. Other than that, you can indeed fiddle with log/exp: return exp(ex * log(nm)); but that has some limitations (for example, does not work for nm <= 0, whereas pow() does) ... and when you could use log() and exp(), why you couldn't use pow().