G
Glen Herrmannsfeldt
Marcus Lessard said:Then maybe it is just me, but I didn't explain myself clearly. The
objective of calculating powers is all well and good and no doubt of use but
what confuses me is the highly specified nature of the algorithm: (from OP):
"...breaking n down into halves(where half of n=n/2), squaring
Power(x,n/2), and multiplying by x again if n was odd..."
Will this be more efficient? Or is it just the solution demanded by the
professor?
Except for some special cases, it is the most efficient set of multiplies to
generate a given power.
In the iterative form, it is the algorithm used by languages that I know of
that supply such an operation.
If n is a power of two, such as 2**m, it will square the number m times.
I suppose as an example for recursive algorithms it is a little better than
factorial.
-- glen