Grade by Grade
C. Algebra · Algebra: Expressions, Equations and Coding

Decomposing Problems: Code with Loops, Equations and Inequalities

C2.2 C2.1 A1About 75 minutes
Learning goals
  • I can decompose a problem into inputs, computational steps and outputs before I write code.
  • I can write a function that solves linear equations, including the special cases.
  • I can use for loops to make tables of values and to search for solutions.
  • I can use while loops and inequalities to find when a condition is first met.

Words to know

decompose
Break a big problem into smaller steps that are each easy to code.
algorithm
A clear, step-by-step set of instructions for solving a problem. (Named after al-Khwarizmi!)
for loop
Repeats code once for each value in a range or list: for n in range(1, 6): runs with n = 1, 2, 3, 4, 5.
while loop
Repeats code as long as a condition is True, and stops as soon as it is False.
range(a, b)
The whole numbers from a up to, but not including, b. range(1, 4) gives 1, 2, 3. range(3) gives 0, 1, 2.

Let's start with a big question

“Write a program that tells a family which snow-removal company to choose.” Where do you even begin?

Programmers almost never start by typing. They decompose the problem: break it into pieces. What goes in? What steps turn the input into an answer? What comes out?

Which snow company is cheaper?INPUTfee, ratesnumber of storms nPROCESScost A = 150 + 25ncost B = 50ncompare A and BOUTPUTmessage:“B is cheaper”
Decomposing the snow-company problem. Each box becomes a few lines of code: variables for the inputs, expressions and an if statement for the process, print for the output.

Turning the algebra steps into code

When you solve ax + b = c by hand, you always do the same two steps: subtract b from both sides, then divide by a. So x = (c − b) ÷ a. That's an algorithm, and it can become a function.

def solve(a, b, c):
# solves a*x + b = c
return (c - b) / a
print(solve(2, 3, 11))

Output: 4.0. This solves 2x + 3 = 11. (Lines starting with # are comments for humans; Python ignores them.)

For equations with x on both sides, ax + b = cx + d, the steps give (a − c)x = d − b, so x = (d − b) ÷ (a − c).

But what if a = c? Then a − c = 0, and dividing by zero is impossible. That's exactly the special case from the solving lesson: no solution, or every x works. Good code handles it.

def solve_both(a, b, c, d):
# solves a*x + b = c*x + d
if a == c:
if b == d:
return "every x"
else:
return "no solution"
return (d - b) / (a - c)
print(solve_both(3, -7, 1, 9))
print(solve_both(2, 6, 2, 5))

Output: 8.0, then no solution. The first call solves the number puzzle 3n − 7 = n + 9; the second is 2x + 6 = 2x + 5.

For loops: tables of values

A for loop repeats lines for each value in a range. It's perfect for making a table of values for an expression, like the toothpick pattern 3n + 1.

for n in range(1, 6):
print(n, 3 * n + 1)

Output: 1 4, 2 7, 3 10, 4 13, 5 16 (one pair per line). range(1, 6) stops before 6.

While loops: when is the goal reached?

A while loop keeps going as long as a condition is true. It answers questions like “How many weeks until I've saved enough?”

Worked example

Example 1: Savings goal

Amir has $120 and saves $35 each week. How many weeks until he has at least $400 for a used snowboard? Solve with a while loop and with an equation.

  1. Decompose. Input: start 120, weekly 35, goal 400. Process: add 35 each week and count weeks while savings < 400. Output: weeks and savings.
  2. Trace: weeks 1 to 8 give 155, 190, 225, 260, 295, 330, 365, 400. At 400 the condition savings < 400 is False, so the loop stops.
  3. Algebra check: 120 + 35w = 400 → 35w = 280 → w = 8 ✓.

The code below prints 8 400: 8 weeks.

savings = 120
weeks = 0
while savings < 400:
savings = savings + 35
weeks = weeks + 1
print(weeks, savings)

Output: 8 400

savings = 120weeks = 0savings< 400 ?yesadd 35 to savingsadd 1 to weeksrepeatnoprint(weeks, savings)
How the while loop runs. It checks the condition, does the indented steps, and goes back to check again. When the condition is false, it leaves the loop.
Worked example

Example 2: Search for the solutions of an inequality

Use a loop to find the integers from −5 to 5 that satisfy 2x + 1 > 5. Compare with algebra.

  1. The loop tests each x and prints it only if the inequality is True.
  2. Algebra: 2x + 1 > 5 → 2x > 4 → x > 2.
  3. Integers from −5 to 5 greater than 2: 3, 4, 5. The loop agrees.

The code below prints 3, 4, 5.

for x in range(-5, 6):
if 2 * x + 1 > 5:
print(x)

Output: 3, 4, 5 (one per line). range(-5, 6) includes −5 and stops before 6.

-5-4-3-2-1012345
The loop's output on a number line: the integers 3, 4 and 5 satisfy 2x + 1 > 5. Algebra says every number greater than 2 works, including decimals like 2.5, which a whole-number loop can't test.

From problem to code

  1. Decompose first: write the inputs, steps and outputs in plain words.
  2. Turn each step into a line or two of code.
  3. Test with a case you already know (like 2x + 3 = 11, where x = 4).
  4. Then test the tricky cases: zero, negatives, and special cases like a = c.
Hands-on activity

The savings jar loop

You need: a jar or bowl, about 40 coins (or buttons), paper and pencil. Optional: a computer with a Python editor.

  1. Choose a start amount, a weekly amount and a goal. Example: start with 5 coins, add 3 each “week”, goal 26 coins.
  2. Run the loop with real coins: before each week, check “Is the jar below the goal?” If yes, add 3 coins and make a tally mark for the week. If no, stop.
  3. Count the weeks and the coins.
  4. Write the matching while loop in Python (copy Example 1 and change the numbers). Trace it or run it.
  5. Solve the matching equation, 5 + 3w = 26, and compare.

Model answer: 5 → 8 → 11 → 14 → 17 → 20 → 23 → 26: 7 weeks, 26 coins. Equation: 3w = 21, w = 7 ✓. If the goal were 27, the loop would stop at 29 coins after 8 weeks, because the jar can't reach exactly 27; the equation gives w = 7.33, so you need 8 whole weeks.

Just for fun

A programmer is asked: “Please buy a carton of milk, and if they have eggs, get a dozen.”

They come home with 12 cartons of milk. (They had eggs.)

Your turn

1

Use the solve_both function: what does solve_both(5, -4, 2, 11) return? Which equation does it solve?

2

Predict the output: for n in range(1, 4): print(2 * n + 5)

3

What does solve(4, -3, 9) return? Check it.

4

A 90 cm snowbank melts 6 cm a day. Predict the output:

height = 90

days = 0

while height > 0: height = height - 6; days = days + 1

print(days)

(On one line, a semicolon separates two instructions.)

5

Predict the output of a loop that starts with for x in range(0, 10): and has this line indented inside it: if 3 * x - 4 <= 8: print(x)

6

Write a for loop that prints the first 5 terms of 4n + 2, starting at n = 1. What does it print?

7

What does solve_both(2, 3, 2, 3) return? Why?

More practice: warm-up

1

Which numbers does range(1, 5) give?

2

Predict: for n in range(3): print(n)

3

Predict: print(solve(2, 0, 10))

4

Decompose a perimeter program: what are the input, process and output?

5

Predict:

total = 0

for n in range(1, 4): total = total + n

print(total)

More practice: on your own

1

Gym A costs 40 + 15m dollars after m months; Gym B costs 25m. Predict the output:

m = 1

while 40 + 15 * m >= 25 * m: m = m + 1

print(m)

2

What does solve_both(1.5, 2, 0.5, 6) return?

3

Write a loop that prints every integer x from −10 to 10 with −2x + 3 > 7. What does it print? Check with algebra.

4

Write a loop that searches s = 0 to 150 for the ticket problem 8s + 12(150 − s) = 1520. What does it print?

5

Why does solve_both check if a == c before dividing?

Challenge

1

Write a loop that finds the snow-company break-even point by searching n = 0 to 20, and confirm it matches the algebra.

2

$500 is invested at 4% interest per year, compounded yearly (each year the amount is multiplied by 1.04). Use a while loop to find how many years until it reaches at least $600. What does your loop print?

3

Predict the output and explain what the code finds:

n = 1

while n * n <= 200: n = n + 1

print(n)

Check your understanding

Quiz

1.

What does it mean to decompose a problem?

2.

Which values does range(1, 4) give?

3.

solve(a, b, c) returns (c − b) / a. What does solve(3, 2, 14) return?

4.

When does a while loop stop?

5.

A loop runs for x in range(0, 6): and, indented inside it, if x + 2 < 5: print(x). What does it print?

6.

Why does solve_both check whether a == c before dividing?

For parents and teachers

Big idea: C2.2 asks students to create code by decomposing situations into computational steps in order to represent mathematical concepts and relationships, and to solve problems. Here students turn the equation-solving algorithm into a function, make tables of values with for loops, and use while loops with inequalities (C2.1) to find when a goal is reached.

Common difficulties:

  • Off-by-one errors with range: range(1, 5) stops at 4.
  • Indentation: lines inside a loop run every time; lines after it run once.
  • Infinite loops: a while loop whose variable never changes never stops. If code seems frozen, check that something in the loop moves toward the stopping condition.
  • Forgetting special cases such as division by zero.

How to help: The savings-jar activity makes loops physical. Ask your teen to explain the decomposition out loud before writing any code: “What goes in? What comes out? What steps are in between?”