Pregunta de entrevista

Entrevista para Senior Software Engineer

-Buenos Aires

MuleSoft

How to write an efficient method to calculate x raise to the power n, implement test method as well

Respuesta

Respuesta de la entrevista

1 respuesta

0

Implement as subroutines of size x/2 and call the subroutines recursively. Implement Memoization, if similar to Fibonacci prefer Dynamic Programming instead. /* calculate x raised to the power y, works for float x and negative y */ #include float power(float x, int y) { float temp; if( y == 0) { return 1; } temp = power(x, y/2); if (y%2 == 0) { return temp*temp; } else { if(y > 0) { return x*temp*temp; } else { return (temp*temp)/x; } } } /* Program to test function power */ int main() { float x = 2; int y = -3; printf("%f", power(x, y)); getchar(); return 0; }

Anónimo en

Agregar respuestas o comentarios

Para comentar esto, Inicia sesión o regístrate.