Problem Efficient Powers - RobAllan27/CodingProblems GitHub Wiki

Problem

This project looks at how to raise an integer to an integer power effectively. Rather than simple naive multiplication, x * x *x * x ..., if x is raised to a large power, this could be a lot of multiplications

Approach

If x^y then look at y , and translate it to a binary number. Then create a stable of power of x up to that number, taking sucessive squares like x^1, x^2, x^4 etc Thus we can get to x^1024 with just 10 multiplications We can the use y, expressed as binary number, and get the relevant entries for the 'stable' of power values.

Possible future optimisation to consider getting a power too high and then dividing, e.g 11011, is like 11111, divided by 100.

Package

package efficientpowers;