Multiplication between 2 integers can be reduced into a series of addition like so
3 * 5 = 3 + 3 + 3 + 3 + 3 = 5 + 5 + 5
Exponentiation (raising a to the power b) can also be reduced into a series of multiplications:
5 ^ 3 = 5 * 5 * 5
Therefore, exponentiation can be reduced into a series of additions, by creating a multiplication expression, then into a series of additions. For example, 5 ^ 3
(5 cubed) can be rewritten as
5 ^ 3 = 5 * 5 * 5
= (5 + 5 + 5 + 5 + 5) * 5
= (5 + 5 + 5 + 5 + 5) + (5 + 5 + 5 + 5 + 5) + (5 + 5 + 5 + 5 + 5) + (5 + 5 + 5 + 5 + 5) + (5 + 5 + 5 + 5 + 5)
= 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5
Your task is, given expressions added together consisting of exponentiation, multiplication and addition, reduce it to the shortest series of additions. The "shortest" expression is defined as the expression with the fewest number of +
symbols, still using only one of the two numbers in the original expression. For example, the shortest expression of 10 * 2
is 10 + 10
.
The numbers involved in the input will all be positive integers, and the expression will consist of only +
(addition), *
(multiplication) and ^
(exponentiation), along with integers and brackets (()
) to indicate precedence.
The output should consist of positive integers and +
symbols only. You shouldn't output the individual steps of the reductions, just the final output. The output may not consist of any numbers that don't appear in the input. However, you may use any 3 distinct symbols instead of +*^
, but please say what symbols they are
The spaces separating inputs and outputs may or may not be used in your programs, i.e. 3 * 5
can be outputted as either 5 + 5 + 5
or 5+5+5
.
Note that in most cases, addition is not actually performed. The only case where addition is to be performed is when you have something like 5 ^ (1 + 2)
, in which case, addition is necessary to continue -> 5 ^ 3 -> 5 * 5 * 5 -> ...
. See test case #4.
Your code does not need to handle inputs that arrive at an ambiguous expression. For example, (2 + 2) * (4 + 1)
. Because of the rules set forth so far, the goal is not to calculate the answer, the goal is to simplify to additions. So the result could be different depending on the order that expressions are resolved or commuted (which additions to simplify, which to leave?). Another invalid example: ((3 + 2) ^ 2) ^ 3 -> ((3 + 2) * (3 + 2)) ^ 3 -> ???
.
This is code-golf so shortest code wins
Test cases
Input => output
5 ^ 3 + 4 * 1 ^ 5 => 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 5 + 4
2 ^ 1 * 2 + 3 + 9 => 2 + 2 + 3 + 9
2 ^ 1 * (2 + 3) + 9 => 2 + 3 + 2 + 3 + 9
2 ^ (1 * (2 + 3)) + 9 => 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 9
10 + 3 * 2 + 33 ^ 2 => 10 + 3 + 3 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33 + 33
100 * 3 => 100 + 100 + 100
2 ^ 1 + 2 ^ 1 + 2 ^ 2 + 8 ^ 1 => 2 + 2 + 2 + 2 + 8
(1 + 2 + 5 * 8 + 2 ^ 4) * 2 => 1 + 2 + 8 + 8 + 8 + 8 + 8 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 1 + 2 + 8 + 8 + 8 + 8 + 8 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
**
instead of^
? \$\endgroup\$using only one of the two numbers in the original expression.
but the original expression can have more than two numbers. I don't get why8 + 8
is not a valid output for2 ^ 1 + 2 ^ 1 + 2 ^ 2 + 8 ^ 1
. This question is still pretty unclear to me. \$\endgroup\$