Given two natural numbers (less than 100) as input print the sequence of intermediate results obtained when computing the sum of the two numbers using only the following operations1:
n <-> (m+1)
for integersn
andm
satisfying that equation(a+b)+c <-> a+(b+c)
for integersa
,b
andc
(associative law)
You are not allowed to use the commutative law (swapping the arguments of +
)
Example
5+3 # Input
5+(2+1) # 3 = 2+1
(5+2)+1 # Associative law
(5+(1+1))+1 # 2 = 1+1
((5+1)+1)+1 # Associative law
(6+1)+1 # 5+1 = 6
7+1 # 6+1 = 7
8 # 7+1 = 8
Rules
- Input: two natural numbers (positive integers)
- Output list of all intermediate steps in the calculation of the sum (using the rules defined above), you may omit the final result
- The expressions can be output in any reasonable format (the left and right operands of each
+
should be clearly determinable) - You can choose the order in which the operations are applied as long as you reach the final result
- Each expression in the output has to be obtained from the previous expression by applying exactly one allowed operation to the previous operation
- This is code-golf the shortest code wins
Test cases
infix notation:
2+2 -> 2+(1+1) -> (2+1)+1 -> 3+1 -> 4
3+1 -> 4
1+3 -> 1+(2+1) -> 1+((1+1)+1) -> (1+(1+1))+1 -> ((1+1)+1)+1 -> (2+1)+1 -> 3+1 -> 4
1+3 -> 1+(2+1) -> (1+2)+1 -> (1+(1+1))+1 -> ((1+1)+1)+1 -> (2+1)+1 -> 3+1 -> 4
5+3 -> 5+(2+1) -> (5+2)+1 -> (5+(1+1))+1 -> ((5+1)+1)+1 -> (6+1)+1 -> 7+1 -> 8
postfix notation:
2 2+ -> 2 1 1++ -> 2 1+ 1+ -> 3 1+ -> 4
3 1+ -> 4
1 3+ -> 1 2 1++ -> 1 1 1+ 1++ -> 1 1 1++ 1+ -> 1 1+ 1+ 1+ -> 2 1+ 1+ -> 3 1+ -> 4
1 3+ -> 1 2 1++ -> 1 2+ 1+ -> 1 1 1++ 1+ -> 1 1+ 1+ 1+ -> 2 1+ 1+ -> 3 1+ -> 4
5 3+ -> 5 2 1++ -> 5 2+ 1+ -> 5 1 1++ 1+ -> 5 1+ 1+ 1+ -> 6 1+ 1+ -> 7 1+ -> 8
1 The operations are based on the formal definition of additon but modified to work without the concept of a successor function.