Pergunta de entrevista da empresa Bloomberg

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

Respostas da entrevista

Sigiloso

17 de fev. de 2015

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

1

Sigiloso

13 de fev. de 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().