<s>[INST] What maths rule allows this expression with powers to be rewritten as below? I have been reading through a programming book and the author asked to calculate the following expression in the program.
$$4*5^3+6*5^2+7*5+8$$
I approached this by expanding the expression like so:
$$(4*5*5*5)+(6*5*5)+(7*5)+8$$
$$500+150+35+8 = 693$$
In his solution he states that the expression can be rewritten as:
$$((4*5+6)*5+7)*5+8$$
$$(26*5+7)*5+8$$
$$(130+7)*5+8$$
$$(137*5)+8$$
$$685+8 = 693$$
Which produces the correct answer, but there is no explanation as to why this works. I wasn't aware the expression could be rewritten like this and the only pattern I can see is that the five to the power of x is decreasing by one each time.
Is there a technical name for this rule? I am curious to know why this works.
 [/INST] The main rule used is the distributivity rule which states that for real $a,b,c$, $$a(b+c)=ab + ac.$$
This rule can easily be used on more than $2$ summands, so say you have a real number $a$ and $n$ real numbers $b_1,\dots, b_n$.
Then,
$$a(b_1+\cdots + b_n) = ab_1 + ab_2 + \cdots + ab_n$$
This means that
$$\begin{align}4\cdot 5^3 + 6\cdot 5^2 + 7\cdot 5 + 8 &= (4\cdot 5^2\cdot 5 + 6\cdot 5\cdot 5 + 7\cdot 5) + 8 \\&= (4\cdot 5^2+6\cdot 5 + 7)\cdot 5 + 8,\end{align}$$
where the second row follows from the first precisely because of the rule written above (and the fact that multiplication is commutative, which is why I can have $5$ on the right side instead of the left).
Using the same rule again on the expression in parentheses, you can see that
$$4\cdot 5^2 + 6\cdot 5 + 7 = 4\cdot 5\cdot 5 + 6\cdot 5 + 7 = (4\cdot 5 + 6)\cdot 5 + 7$$
</s>