In the book Algorithms for interviewers, there is such a question:
How would you determine the minimum number of multiplications to evaluate X to the power 30?
Can anyone tell me what this question means?
What means “evaluate X to the power 30”?
There are two possible meanings, i guess:
- we have a X, then this question asks me “how many multiplications do I need to calculate X^30?”.
- We have a X, then it asks me “What y is so that y^30 = X, and how many multiplications do you need to calculate y?”
I don’t know which of what I guess is correct, or it has a 3rd meaning?
Thanks
The question supposes that there is a way to write a function:
using only multiplications.
In fact one way would be the following:
This uses 30 multiplications.
However consider this:
This uses 16 multiplications.
So the question is asking what is the minimum number of multiplications possible?
Here is 6 multiplications, and likely the interviewers ideal solution:
This is more a brainteaser than a programming problem. A real power function does not use multiplication (or at least not in the way implied by the question)