Sub topic 7
Nested Operations: Work from the Inside Out
What this chapter assumes
You should already be comfortable with these skills from earlier chapters:
- Order of operations, including exponents and parentheses.
- Reading a made-up symbol such as ★ as a rule, not as standard math.
- Substituting values into a definition, including negative and fractional inputs.
- Keeping argument order straight, so that a★b and b★a are treated as different things.
This chapter uses one running definition: a★b = 2a + b². The first input is doubled, and the second input is squared. Keep that asymmetry in mind, because nesting makes it matter more.
What "nested" means
An operation is nested when one of its inputs is itself the result of another operation. The GRE signals this with parentheses:
- (1★2)★3: the inner operation 1★2 supplies the first input of the outer operation.
- 3★(1★2): the same inner operation supplies the second input of the outer operation.
- (1★2)★(3★1): two inner operations supply both inputs.
Parentheses mark the order of evaluation. Nothing about nesting requires new math. It only requires that you do one substitution at a time and never skip ahead.
The inside-out rule
Evaluate the innermost parentheses first. Replace that whole expression with its single numeric value. Then treat the result as an ordinary number in the next operation out.
Here is (1★2)★3 with a★b = 2a + b².
- Inner: 1★2 = 2(1) + 2² = 2 + 4 = 6.
- Rewrite the expression with the value in place: 6★3.
- Outer: 6★3 = 2(6) + 3² = 12 + 9 = 21.
The code below follows the same two steps.
def star(a, b):
# Defined operation: a star b = 2a + b^2
return 2 * a + b ** 2
inner = star(1, 2) # 2*1 + 2**2 = 6
result = star(inner, 3) # 2*6 + 3**2 = 21
print(inner, result) # 6 21The same idea is often written as a single call, star(star(1, 2), 3). Python also evaluates the innermost call first, so the structure of the code mirrors the structure of the math.
Put the inner result in the correct slot
The most common nesting mistake is placing the inner value in the wrong position. The slot decides what happens to the value. In a★b = 2a + b², a value in the first slot is doubled, and a value in the second slot is squared.
Use the same inner result, 6, in each slot:
- (1★2)★3 puts 6 in the first slot: 6★3 = 12 + 9 = 21.
- 3★(1★2) puts 6 in the second slot: 3★6 = 6 + 36 = 42.
The two expressions use the same three numbers and the same inner operation, but they give 21 and 42.
def star(a, b):
return 2 * a + b ** 2
print(star(star(1, 2), 3)) # 6 in the first slot: 21
print(star(3, star(1, 2))) # 6 in the second slot: 42Parentheses can change which operation is inner
With the same three numbers, moving the parentheses to the other pair changes the answer again. Compare (1★2)★3 with 1★(2★3).
- (1★2)★3 = 6★3 = 21, as computed above.
- 1★(2★3): the inner operation is 2★3 = 4 + 9 = 13. The outer operation is 1★13 = 2 + 169 = 171.
def star(a, b):
return 2 * a + b ** 2
left = star(star(1, 2), 3) # (1 star 2) star 3
right = star(1, star(2, 3)) # 1 star (2 star 3)
print(left, right) # 21 171Because 21 ≠ 171, this operation is not associative. A later chapter covers that property formally. For now, the practical point is that you cannot move parentheses around, and you cannot drop them, even if you only want to "read left to right."
Two inner operations at once
When both inputs are nested, evaluate each inner operation separately, then combine. Take (1★2)★(3★1).
- Left inner: 1★2 = 2 + 4 = 6.
- Right inner: 3★1 = 6 + 1 = 7.
- Outer: 6★7 = 12 + 49 = 61.
The two inner operations do not depend on each other, so you can compute them in either order. Write both values down before starting the outer step.
def star(a, b):
return 2 * a + b ** 2
left_inner = star(1, 2) # 6
right_inner = star(3, 1) # 7
print(star(left_inner, right_inner)) # 2*6 + 7**2 = 61Negative values in the middle of a chain
A nested expression can produce a negative intermediate value. That value must go into the next step with its sign, and if it lands in the squared slot, the square applies to the whole number.
Evaluate (−3★2)★3 with a★b = 2a + b².
- Inner: (−3)★2 = 2(−3) + 2² = −6 + 4 = −2.
- Outer: (−2)★3 = 2(−2) + 3² = −4 + 9 = 5.
Now put a negative value in the squared slot: 3★(−3★2) uses the same inner value, −2.
- Outer: 3★(−2) = 2(3) + (−2)² = 6 + 4 = 10.
Writing −2² would give −4 and produce 2, which is wrong. Python shows the same trap.
def star(a, b):
return 2 * a + b ** 2
inner = star(-3, 2) # -2
print(star(inner, 3)) # first slot: 5
print(star(3, inner)) # second slot: 10
# The trap: without parentheses, the exponent binds tighter than the minus sign
print(-2 ** 2) # -4
print((-2) ** 2) # 4When you work by hand, write a negative intermediate value in parentheses every time you substitute it, as in 2(−2) + 3². This one habit prevents most sign errors.
Fractions in the middle of a chain
Fractional intermediate values follow the same rule. Compute (½★1)★2:
- Inner: ½★1 = 2(½) + 1² = 1 + 1 = 2.
- Outer: 2★2 = 4 + 4 = 8.
Fractions in the squared slot need the square applied to both numerator and denominator. For example, 1★(½) = 2 + (½)² = 2 + ¼ = 2¼. The GRE rarely makes the arithmetic hard. The difficulty is organizing the steps, so write every intermediate value instead of carrying it in your head.
Nesting function notation
The same method works when the rule uses function notation. If f(x) = x² − 1, then f(f(2)) means "apply f to the output of f(2)."
- Inner: f(2) = 2² − 1 = 3.
- Outer: f(3) = 3² − 1 = 8.
def f(x):
return x ** 2 - 1
print(f(f(2))) # f(3) = 8A single-input function has only one slot, so there is no slot-order risk. Repeated application is still easy to botch: write each stage on its own line.
Solving when an unknown is nested
A nested expression can contain a variable, and the question can give the final value. First simplify the inner operation symbolically, then solve for the unknown.
Suppose (x★1)★1 = 15.
- Inner: x★1 = 2x + 1² = 2x + 1.
- Outer: (2x + 1)★1 = 2(2x + 1) + 1 = 4x + 3.
- Set equal: 4x + 3 = 15, so x = 3.
The outer step is where the algebra can go wrong. The whole inner expression 2x + 1 goes into the first slot, so the doubling must cover both terms. Writing 2·2x + 1 would lose the +1 that came from the inner step.
Check by substituting x = 3 back in: 3★1 = 7, and 7★1 = 14 + 1 = 15. ✓
def star(a, b):
return 2 * a + b ** 2
x = 3
print(star(star(x, 1), 1)) # 15, so x = 3 checks outIf the unknown sits in the squared slot, the equation becomes quadratic and can have two solutions. For example, 1★x = 11 gives 2 + x² = 11, so x² = 9 and x = 3 or x = −3. Read the question to see whether it asks for all values, the positive value, or a quantity such as x². The next chapter covers this kind of backward reasoning in detail.
Common errors
| Error | Example with a★b = 2a + b² | Fix |
|---|---|---|
| Doing the outer operation first | Treating (1★2)★3 as if 2★3 happens first | Find the innermost parentheses and evaluate those |
| Wrong slot for the inner value | Computing 3★6 for (1★2)★3 | The inner value is the first input here, so use 6★3 |
| Lost sign when squaring | −2² = −4 for a squared input of −2 | Write (−2)² = 4 |
| Doubling only part of an inner expression | 2·2x + 1 instead of 2(2x + 1) | Put parentheses around the whole substituted expression |
| Moving parentheses | Treating 1★(2★3) as the same as (1★2)★3 | Evaluate in the order the parentheses give |
| Using the standard meaning of ★ | Reading it as multiplication | Use only the given definition |
A checking routine for nested expressions
- Rewrite the definition in a template with blank slots: 2(_) + (_)².
- Circle the innermost parentheses and evaluate only that part.
- Write the result on its own line, with its sign, in parentheses if negative.
- Substitute it into the correct slot of the next-level template.
- Repeat until no parentheses remain.
- Estimate to catch gross errors. If the final slot is squared, a result near the square of the second input should not be negative.
Exercise
With a★b = 2a + b², evaluate (2★1)★(−1).
Show answer
Inner: 2★1 = 2(2) + 1² = 4 + 1 = 5. Outer: 5★(−1) = 2(5) + (−1)² = 10 + 1 = 11. The answer is 11.
Exercise
With a★b = 2a + b², find x if (x★2)★0 = 14.
Show answer
Inner: x★2 = 2x + 2² = 2x + 4. Outer: (2x + 4)★0 = 2(2x + 4) + 0² = 4x + 8. Set 4x + 8 = 14, so x = 3/2. Check: (3/2)★2 = 3 + 4 = 7, and 7★0 = 14 + 0 = 14.
Exercise
If f(x) = x² − 1, what is f(f(f(1)))?
Show answer
f(1) = 1 − 1 = 0. f(0) = 0 − 1 = −1. f(−1) = (−1)² − 1 = 1 − 1 = 0. The answer is 0.
Exercise
With a★b = 2a + b², compute 2★(1★(−1)). Then say whether it equals (2★1)★(−1) from the first exercise.
Show answer
Inner: 1★(−1) = 2(1) + (−1)² = 2 + 1 = 3. Outer: 2★3 = 4 + 9 = 13. The first exercise gave 11, and 13 ≠ 11, so moving the parentheses changed the result.