I am reading an algorithm that calculates $x^y$. Basically it is about an implementation of a function $power(x, y)$ where $x$ is the base and $y$ is the exponent i.e. the power.
The algorithm uses successive powers of two and is as follows:
result = 1
while y not equal to 0
if y is not even
result = result * x
x = x * x
shift y 1 bit to the right
end while
return result
The check for if y is not even is by checking the least significant bit.
In any case this algorithm seems to work in all the test cases and from what I see it comes down to multiplying powers of $2$ with $x$.
E.g. for $x^7$ since $7 = 111$ we essentially end up with:
$result = x\cdot x^2\cdot x^4$
Now I can see the connection with the binary representation since if we successively square the exponent increases by powers of $2$ and hence that shift helps with the successive squaring.
So what I want to ask is the following: I think the underlying assumption is that any number can be broken to successive powers of $2$ plus one i.e. $2^n + 1$ since $x^7 = x^{1 + 2 + 4}$.
So is there a formal principle for this i.e. same as we have prime decomposition?
Clearly, the number should be a natural number. So, if it is a natural number, assume WLOG that it is even. (If it is odd, then it is 1 + some even number). Now, your question reduces to, can every even number be reduced to the sum of powers of 2? Note that an even number is 2n, for some $n \in \mathbb{Z}^+$. Thus, an even number is 2 + 2 + ... 2, summed n times. Now, your claim holds if, $\forall n \in \mathbb{Z}^+, 2n = \sum_{\alpha \in I} 2^\alpha$, for $I \subset \mathbb{N}$.
We will prove this by induction. For n = 1, $2 = 2$, so it is trivially true. Assume true for $n = k$ that $2k = \sum_{\alpha \in I} 2^\alpha$, where $I$ has some arbitrary maximum number $m$, say (it must have one, since $N$ is well-ordered). Then, for $n = k+1$, we have $2(k+1) = 2k + 2 = (\sum_{\alpha \in I} 2^\alpha) + 2$. Now, if $1 \notin I$, then our claim holds. However, if $1 \in I$, then our sum becomes $4 + \sum_{\alpha \in I, \alpha \neq 1} 2^\alpha$. Again, if $2 \notin I$, our claim holds. This argument can be repeated for $ 1 \leq j \leq m $ to show that, if for some such $j$, $j \notin I$, then our claim holds.
However, assume that our set $I$ includes all integers from $1$ through $m$. Then, our number is of the form $2 + 4 + 8 + ... + 2^m + 2$. But note that this is just $2^{m+1}$, since $2 + 2 + 4 + 8 + ... + 2^m = 2 + 2(2^m - 1) = 2^{m+1}$, by applying the geometric series formula. QED.