1. Variables and Expressions
What Are Variables?
A variable is a letter (like x, y, or n) that stands in for an unknown number. Think of it as a labeled box that can hold any value. In the expression x + 5, the variable x could be 1, 7, -3, or any other number -- we just don't know which one yet.
Variables let us write general rules that work for any number, not just a specific one. Instead of saying "3 plus 5 is 8," we can say "x + 5" and it works for every possible value of x.
You use variables constantly without realizing it:
- "I'll be there in t minutes" -- t is a variable for time
- "Speed limit is s mph" -- s varies by road
- "The bill comes to $x" -- x depends on what you ordered
- "There are n people coming" -- n is unknown until people RSVP
Variables in algebra are exactly the same idea as variables in programming. When you write let x = 10; in JavaScript or x = 10 in Python, you're creating a named container that holds a value -- just like in algebra. Understanding algebraic variables makes programming variables intuitive.
Algebraic Expressions
An algebraic expression is a mathematical phrase that contains numbers, variables, and operations. Here are the key vocabulary words:
- Term -- a single chunk separated by + or - signs. In
3x + 7, the terms are3xand7. - Coefficient -- the number multiplied by the variable. In
3x, the coefficient is 3. - Constant -- a term with no variable. In
3x + 7, the constant is 7. - Like terms -- terms with the same variable and exponent.
3xand5xare like terms.3xand3x²are NOT like terms.
Simplifying Expressions
To simplify an expression, combine like terms by adding or subtracting their coefficients.
Simplify: 4x + 3 + 2x - 1
Step 1: Group the like terms: (4x + 2x) + (3 - 1)
Step 2: Combine: 6x + 2
Answer: 6x + 2
If you're struggling with like terms, try color-coding or underlining. Use one color for all x terms, another for all x² terms, and another for constants. Only combine terms of the same color!
1) Simplify: 5a + 2b + 3a - b
Group: (5a + 3a) + (2b - b) = 8a + b
2) Simplify: 2x² + 4x + x² - 2x + 7
Group: (2x² + x²) + (4x - 2x) + 7 = 3x² + 2x + 7
3) Simplify: -3y + 5 + 7y - 2 + y
Group: (-3y + 7y + y) + (5 - 2) = 5y + 3
Key insight: Only combine terms with identical variable parts. 3x and 3x² cannot be combined!
The Distributive Property
The distributive property says that multiplying a number by a group of numbers added together is the same as multiplying the number by each one individually and then adding the results. You "distribute" the multiplication across each term inside the parentheses.
Expand: 3(2x + 4)
Step 1: Multiply 3 by each term inside: 3 * 2x + 3 * 4
Step 2: Simplify: 6x + 12
Answer: 6x + 12
Simplify: 2(x + 3) + 4x
Step 1: Distribute: 2x + 6 + 4x
Step 2: Combine like terms: 6x + 6
Answer: 6x + 6
1) Expand: 4(2x - 3)
4 × 2x + 4 × (-3) = 8x - 12
2) Expand: -3(x + 5)
-3 × x + (-3) × 5 = -3x - 15
3) Expand: 0.5(4y - 6)
0.5 × 4y + 0.5 × (-6) = 2y - 3
4) Expand and simplify: 3(2x + 1) - 2(x - 4)
= 6x + 3 - 2x + 8
= (6x - 2x) + (3 + 8) = 4x + 11
Don't forget to distribute to every term inside the parentheses. A common error: writing 3(2x + 4) = 6x + 4 instead of the correct 6x + 12. The 3 must multiply both the 2x AND the 4.
Distributing with Fractions
Distribution works the exact same way when fractions are involved. Your brain might panic, but the rule hasn't changed: multiply by each term inside the parentheses.
It doesn't matter if a, b, or c are fractions, negatives, or have exponents. Same rule.
This is the kind of expression that shows up in Big-O analysis. Let's go step by step.
Step 1: Identify what you're distributing
a = 3n, b = 40, c = n³/2
Step 2: Distribute -- multiply 3n by each term
= 3n × 40 + 3n × (n³/2)
Step 3: Simplify each part
First part: 3n × 40 = 120n
Second part: 3n × n³ = 3n4, then ÷ 2 = 3n4/2
Result: 120n + 3n4/2
This is n(n+1)/2 written differently. Let's distribute:
= (n/2) × n + (n/2) × 1
= n²/2 + n/2
= (n² + n) / 2
This is the expanded form of n(n+1)/2 -- the sum of 1 to n.
-(x/3 + 2/5)
Distribute the -1:
= (-1) × x/3 + (-1) × 2/5
= -x/3 - 2/5
Splitting Fractions (When You Can and Can't)
This is the source of so much confusion. Here's the exact rule:
(a + b) / c = a/c + b/c ✓
Splitting the DENOMINATOR -- NEVER valid:
c / (a + b) ≠ c/a + c/b ✗
The denominator is a single term (just 2), so we can split the numerator:
(40 + n³) / 2 = 40/2 + n³/2 = 20 + n³/2
Why does this work? Division distributes over addition in the numerator. Think of dividing a pizza: if you split 8 slices among 2 people, each person gets 4. If you split (5 + 3) slices among 2 people, each gets 5/2 + 3/2 = 2.5 + 1.5 = 4. Same result.
12 / (x + 4) ≠ 12/x + 12/4
If x = 2: Left side = 12/6 = 2. Right side = 6 + 3 = 9. Not even close!
When the denominator has addition/subtraction, you cannot split. The fraction must stay as one piece, or you need to find another approach.
Common Denominator: Making Fractions Combine
When you need to add a whole number to a fraction, convert the whole number to a fraction first:
Step 1: Write 40 as 40/1
Step 2: Common denominator is 2, so: 40/1 = 80/2
Step 3: Add: 80/2 + n³/2 = (80 + n³) / 2
This is useful when you want to simplify an expression into a single fraction.
You have 3n × 40/2. How to compute this?
Method 1 (just multiply): 3n × 40/2 = (3n × 40) / 2 = 120n/2 = 60n
Method 2 (simplify first): 40/2 = 20, so 3n × 20 = 60n
Both methods give the same answer. Use whichever feels simpler.
2. Solving Linear Equations
An equation is a statement that two expressions are equal, connected by an equals sign. Solving an equation means finding the value of the variable that makes the statement true.
This is the single most important idea in equation solving. An equation is like a perfectly balanced scale. If you add 5 to the left side, you must add 5 to the right side too, or the scale tips over and the equation becomes false.
One-Step Equations
These require a single operation to isolate the variable.
Solve: x + 5 = 12
Goal: Get x by itself.
Subtract 5 from both sides: x + 5 - 5 = 12 - 5
Result: x = 7
Check: 7 + 5 = 12. Correct!
We don't actually move numbers. We add the same amount to both sides to keep the equation balanced. Saying "move 5 to the other side" is shorthand for "subtract 5 from both sides." The goal is always to isolate the variable.
Solve: 3x = 21
Divide both sides by 3: 3x / 3 = 21 / 3
Result: x = 7
Check: 3(7) = 21. Correct!
Two-Step Equations
Strategy: undo addition/subtraction first, then undo multiplication/division.
Solve: 2x + 3 = 11
Step 1: Subtract 3 from both sides: 2x = 8
Step 2: Divide both sides by 2: x = 4
Check: 2(4) + 3 = 8 + 3 = 11. Correct!
1) Solve: 3x - 4 = 14
Add 4: 3x = 18
Divide by 3: x = 6
Check: 3(6) - 4 = 18 - 4 = 14 ✓
2) Solve: -2x + 7 = 1
Subtract 7: -2x = -6
Divide by -2: x = 3
Check: -2(3) + 7 = -6 + 7 = 1 ✓
3) Solve: 5 + 4y = 21
Subtract 5: 4y = 16
Divide by 4: y = 4
Check: 5 + 4(4) = 5 + 16 = 21 ✓
To solve 2x + 3 = 11, ask yourself: "What operations were done to x?" Answer: "First multiplied by 2, then added 3." To undo this, work backwards: first subtract 3, then divide by 2. Always undo addition/subtraction before multiplication/division.
Solve: -5x + 10 = -15
Step 1: Subtract 10 from both sides: -5x = -25
Step 2: Divide both sides by -5: x = 5
Check: -5(5) + 10 = -25 + 10 = -15. Correct!
Multi-Step Equations (Variables on Both Sides)
When the variable appears on both sides, move all variable terms to one side and all constants to the other.
Don't worry if x comes out negative! Many students think negative answers are "wrong," but they're often correct. For example, if x - 3 = -8, then x = -5. Negative numbers are perfectly valid solutions. Always check your answer by substituting back.
Solve: 5x + 3 = 2x + 18
Step 1: Subtract 2x from both sides: 3x + 3 = 18
Step 2: Subtract 3 from both sides: 3x = 15
Step 3: Divide both sides by 3: x = 5
Check: 5(5) + 3 = 28, and 2(5) + 18 = 28. Correct!
1) Solve: 4x + 1 = x + 10
Subtract x: 3x + 1 = 10
Subtract 1: 3x = 9
Divide by 3: x = 3
2) Solve: 6y - 5 = 4y + 7
Subtract 4y: 2y - 5 = 7
Add 5: 2y = 12
Divide by 2: y = 6
3) Solve: 3a + 8 = 7a - 4
Subtract 3a: 8 = 4a - 4
Add 4: 12 = 4a
Divide by 4: a = 3
Solve: 3(x - 2) = 2x + 1
Step 1: Distribute: 3x - 6 = 2x + 1
Step 2: Subtract 2x from both sides: x - 6 = 1
Step 3: Add 6 to both sides: x = 7
Check: 3(7 - 2) = 3(5) = 15, and 2(7) + 1 = 15. Correct!
Equations with Fractions
The trick: multiply every term by the least common denominator (LCD) to clear all the fractions first. Then solve normally.
Solve: (x/2) + (x/3) = 5
The LCD of 2 and 3 is 6. Multiply every term by 6:
6(x/2) + 6(x/3) = 6(5)
3x + 2x = 30
5x = 30
x = 6
Check: 6/2 + 6/3 = 3 + 2 = 5. Correct!
When clearing fractions, you must multiply every single term by the LCD -- including terms that aren't fractions. A very common error is only multiplying the fraction terms and forgetting the constant on the other side.
Solving equations is essentially what computers do when they evaluate expressions and solve algorithms. When you write code that calculates a value from a formula -- say, converting Celsius to Fahrenheit with f = (9/5) * c + 32 -- you're applying the same equation-solving logic. Understanding how to rearrange formulas is also key for deriving algorithms, analyzing complexity, and optimizing code.
3. Inequalities
An inequality is like an equation, but instead of saying two things are equal, it says one is bigger or smaller. The four inequality symbols are:
| Symbol | Meaning | Example |
|---|---|---|
| < | Less than | x < 5 (x is less than 5) |
| > | Greater than | x > 3 (x is greater than 3) |
| ≤ | Less than or equal to | x ≤ 10 (x is at most 10) |
| ≥ | Greater than or equal to | x ≥ 0 (x is non-negative) |
Solving Inequalities
You solve inequalities almost exactly like equations, with one critical exception: when you multiply or divide both sides by a negative number, you must flip the inequality sign.
Solve: 2x + 1 < 9
Step 1: Subtract 1 from both sides: 2x < 8
Step 2: Divide both sides by 2: x < 4
Solution: x can be any number less than 4.
Solve: -3x + 6 ≤ 15
Step 1: Subtract 6 from both sides: -3x ≤ 9
Step 2: Divide both sides by -3 (flip the sign!): x ≥ -3
Solution: x can be -3 or any number greater than -3.
Forgetting to flip the inequality sign when multiplying or dividing by a negative number. This is the number one mistake students make with inequalities. Ask yourself: "Am I dividing by a negative?" If yes, flip the sign.
Graphing Inequalities on a Number Line
You can visualize inequality solutions on a number line:
- x < 4 -- Draw an open circle at 4 (meaning 4 is NOT included) and shade everything to the left.
- x ≥ -3 -- Draw a filled circle at -3 (meaning -3 IS included) and shade everything to the right.
- Open circle = strict inequality (< or >), filled circle = includes the endpoint (≤ or ≥).
Inequalities show up constantly in programming: loop conditions (while i < n), array bounds checking (if index >= 0 && index < length), and algorithm analysis ("this algorithm runs in O(n) when n ≥ 1"). Understanding how inequalities work helps you reason about when loops terminate and whether your array accesses are safe.
4. Functions
What Is a Function?
A function is an input-output machine: you feed it an input, it follows a rule, and it produces exactly one output. The key idea is that each input gives exactly one output. If you put the same number in twice, you get the same result both times.
We usually write functions using the notation f(x), which reads as "f of x." The letter f is the name of the function, and x is the input. For example:
This function takes any number x, doubles it, then adds 3.
If f(x) = 2x + 3, find f(4).
Replace x with 4: f(4) = 2(4) + 3 = 8 + 3 = 11
Answer: f(4) = 11
Domain and Range
- Domain -- the set of all valid inputs (x-values). Ask: "What values of x can I plug in without breaking anything?"
- Range -- the set of all possible outputs (y-values). Ask: "What values can actually come out?"
For example, the function f(x) = 1/x has a domain of all real numbers except 0 (because dividing by zero is undefined). Its range is also all real numbers except 0 (because 1/x never equals zero).
Linear Functions: y = mx + b
The most important type of function in beginning algebra is the linear function. Its graph is a straight line.
m = slope (steepness and direction of the line)
b = y-intercept (where the line crosses the y-axis)
- Slope (m) tells you how much y changes for each 1-unit increase in x. A slope of 2 means "go up 2 for every 1 you go right." A negative slope means the line goes downhill.
- Y-intercept (b) is the y-value when x = 0. It's where the line crosses the vertical axis.
For the line y = -3x + 7:
Slope: m = -3 (the line goes down 3 units for every 1 unit to the right)
Y-intercept: b = 7 (the line crosses the y-axis at the point (0, 7))
Slope Formula
Given two points on a line, (x1, y1) and (x2, y2), the slope is:
This is often described as "rise over run" -- the vertical change divided by the horizontal change.
Find the slope of the line through (1, 2) and (4, 11).
m = (11 - 2) / (4 - 1) = 9 / 3 = 3
The slope is 3. The line rises 3 units for every 1 unit to the right.
Understanding Slope Intuitively
Positive slope: Line goes upward left-to-right (like climbing a hill)
• m = 2: For every 1 step right, go up 2 steps
• m = 0.5: For every 1 step right, go up 0.5 steps (gentle incline)
Negative slope: Line goes downward left-to-right (like descending a hill)
• m = -1: For each 1 step right, go down 1 step (45° decline)
• m = -3: For each 1 step right, go down 3 steps (steep decline)
Zero slope: m = 0 means horizontal line (no rise or fall)
Undefined slope: Vertical line (infinite steepness)
Finding the Equation of a Line
Write the equation of a line with slope -2 and y-intercept 5:
Use y = mx + b directly: y = -2x + 5
Find the equation passing through (3, 7) and (5, 13):
Step 1: Find slope: m = (13 - 7)/(5 - 3) = 6/2 = 3
Step 2: Use point-slope form with either point. Using (3, 7):
y - 7 = 3(x - 3)
y - 7 = 3x - 9
y = 3x - 2
Answer: y = 3x - 2
Check with other point: 3(5) - 2 = 15 - 2 = 13 ✓
Find the equation with slope 4 passing through (-1, 3):
Point-slope form: y - y₁ = m(x - x₁)
y - 3 = 4(x - (-1))
y - 3 = 4(x + 1)
y - 3 = 4x + 4
y = 4x + 7
Graphing Lines Step-by-Step
For y = 2x - 3:
1. Plot the y-intercept: (0, -3)
2. Use slope 2 = 2/1 to find next point: from (0, -3), go right 1 and up 2 to reach (1, -1)
3. Continue: from (1, -1) go right 1 and up 2 to reach (2, 1)
4. Draw line through these points
For y = -x + 4:
1. Choose any x-values, say x = 0 and x = 4
2. When x = 0: y = -(0) + 4 = 4, so (0, 4)
3. When x = 4: y = -(4) + 4 = 0, so (4, 0)
4. Plot these points and draw the line
Special Forms of Linear Equations
Point-Slope Form: y - y₁ = m(x - x₁)
Slope-Intercept Form: y = mx + b
Intercept Form: x/a + y/b = 1
Convert 3x + 2y = 6 to slope-intercept form:
Solve for y:
2y = -3x + 6
y = (-3x + 6)/2
y = -1.5x + 3
So slope = -1.5, y-intercept = 3
Functions in algebra map directly to functions in programming. The concept of domain is like input validation -- what inputs are acceptable? In graphics programming, linear functions define lines on screen. Slope appears in machine learning as the "weight" in linear regression models. And the idea that a function always produces the same output for the same input is the foundation of "pure functions" in functional programming.
5. Systems of Equations
A system of equations is a set of two (or more) equations with the same variables. The solution is the values of the variables that satisfy ALL equations simultaneously. Geometrically, for two linear equations, the solution is where the two lines intersect.
Method 1: Substitution
Idea: Solve one equation for one variable, then substitute that expression into the other equation.
Solve the system:
y = 2x + 1 ... (Equation 1)
3x + y = 16 ... (Equation 2)
Step 1: Equation 1 already has y isolated: y = 2x + 1
Step 2: Substitute into Equation 2:
3x + (2x + 1) = 16
5x + 1 = 16
5x = 15
x = 3
Step 3: Plug x = 3 back into Equation 1:
y = 2(3) + 1 = 7
Solution: (x, y) = (3, 7)
Check in Equation 2: 3(3) + 7 = 9 + 7 = 16. Correct!
Method 2: Elimination
Idea: Add or subtract the equations to eliminate one variable.
Solve the system:
2x + 3y = 12 ... (Equation 1)
4x - 3y = 6 ... (Equation 2)
Step 1: Notice that 3y and -3y will cancel if we add the equations.
Step 2: Add Equation 1 + Equation 2:
(2x + 4x) + (3y - 3y) = 12 + 6
6x = 18
x = 3
Step 3: Plug x = 3 into Equation 1:
2(3) + 3y = 12
6 + 3y = 12
3y = 6
y = 2
Solution: (x, y) = (3, 2)
Check in Equation 2: 4(3) - 3(2) = 12 - 6 = 6. Correct!
Solve the system:
3x + 2y = 16 ... (Equation 1)
x + 4y = 22 ... (Equation 2)
Step 1: The variables don't cancel yet. Multiply Equation 2 by -3 so the x terms cancel:
3x + 2y = 16
-3x - 12y = -66
Step 2: Add the equations:
-10y = -50
y = 5
Step 3: Plug y = 5 into Equation 2:
x + 4(5) = 22
x + 20 = 22
x = 2
Solution: (x, y) = (2, 5)
Substitution is easiest when one variable is already isolated (like y = ...) or has a coefficient of 1. Elimination is easiest when the coefficients line up nicely or can be made to cancel with simple multiplication.
Systems of equations are everywhere in CS. In computer graphics, finding where two lines intersect is solving a 2-variable system. Linear programming (used for optimization problems like scheduling and resource allocation) involves solving systems of equations and inequalities. Machine learning's linear regression solves systems of equations to find the best-fit line. At scale, these become matrix equations -- which is why linear algebra matters so much for CS.
Understanding Solutions Graphically
The solution to a system of two linear equations is the intersection point of the two lines. This point satisfies both equations simultaneously.
One Solution (Most Common): Lines intersect at exactly one point
• Example: y = 2x + 1 and y = -x + 4 intersect at (1, 3)
No Solution: Lines are parallel (never intersect)
• Example: y = 2x + 1 and y = 2x + 5 (same slope, different y-intercepts)
Infinite Solutions: Lines are identical (overlap completely)
• Example: y = 2x + 1 and 2y = 4x + 2 (same line written differently)
More Practice Examples
Solve: x + y = 5 and 2x - y = 1
From equation 1: y = 5 - x
Substitute into equation 2: 2x - (5 - x) = 1
2x - 5 + x = 1
3x = 6
x = 2
So y = 5 - 2 = 3
Solution: (2, 3)
Check: 2 + 3 = 5 ✓ and 2(2) - 3 = 1 ✓
Solve: 2x + 3y = 7 and 4x - 3y = 5
Notice the y-coefficients are opposites (3 and -3), so add directly:
(2x + 4x) + (3y - 3y) = 7 + 5
6x = 12
x = 2
Substitute back: 2(2) + 3y = 7 → 4 + 3y = 7 → y = 1
Solution: (2, 1)
Concert tickets cost $15 for adults and $8 for children. If 200 tickets were sold for $2,250 total, how many of each type were sold?
Let a = adult tickets, c = child tickets
Total tickets: a + c = 200
Total revenue: 15a + 8c = 2250
From equation 1: c = 200 - a
Substitute: 15a + 8(200 - a) = 2250
15a + 1600 - 8a = 2250
7a = 650
a = ~93 adult tickets, c = ~107 child tickets
6. Polynomials
What Are Polynomials?
A polynomial is an expression made up of terms where variables are raised to non-negative integer powers. Each term has a coefficient and a variable part. Here are some examples:
5x + 3-- a polynomial with 2 terms (a binomial)x² + 4x - 7-- a polynomial with 3 terms (a trinomial)2x³ - x² + 5x - 1-- a polynomial with 4 terms
The degree of a polynomial is the highest exponent. The degree of x² + 4x - 7 is 2. The degree of 2x³ - x² + 5x - 1 is 3.
Adding and Subtracting Polynomials
Simply combine like terms (terms with the same variable and exponent).
(3x² + 2x + 1) + (x² - 5x + 4)
Combine like terms:
x² terms: 3x² + x² = 4x²
x terms: 2x + (-5x) = -3x
Constants: 1 + 4 = 5
Answer: 4x² - 3x + 5
(5x² + 3x - 2) - (2x² - x + 6)
Distribute the negative sign: 5x² + 3x - 2 - 2x² + x - 6
Combine like terms: 3x² + 4x - 8
Answer: 3x² + 4x - 8
When subtracting polynomials, remember to distribute the negative sign to every term in the second polynomial. A common error: (5x² + 3x - 2) - (2x² - x + 6) = 3x² + 2x + 4. The mistake is not flipping -x to +x and +6 to -6.
Multiplying Polynomials (FOIL)
To multiply two binomials, use FOIL: First, Outer, Inner, Last.
Multiply: (x + 3)(x + 5)
First: x * x = x²
Outer: x * 5 = 5x
Inner: 3 * x = 3x
Last: 3 * 5 = 15
Combine: x² + 5x + 3x + 15 = x² + 8x + 15
Answer: x² + 8x + 15
Multiply: (2x - 1)(x + 4)
First: 2x * x = 2x²
Outer: 2x * 4 = 8x
Inner: -1 * x = -x
Last: -1 * 4 = -4
Combine: 2x² + 8x - x - 4 = 2x² + 7x - 4
Answer: 2x² + 7x - 4
FOIL only works for two binomials. For larger polynomials, use the general rule: multiply every term in the first polynomial by every term in the second, then combine like terms. FOIL is just a shortcut for this process when each polynomial has exactly two terms.
Special Products (Memorize These!)
These patterns appear so often that memorizing them will save you time and reduce errors.
Perfect Square Trinomials
(a - b)² = a² - 2ab + b²
(x + 5)² = x² + 2(x)(5) + 5²
= x² + 10x + 25
(3y - 2)² = (3y)² - 2(3y)(2) + 2²
= 9y² - 12y + 4
(x + 5)² ≠ x² + 25!
You MUST include the middle term (2ab). This is one of the most common algebra mistakes.
Difference of Squares
= x² - 7² = x² - 49
The middle terms cancel out because one is +7x and one is -7x.
Sum and Difference of Cubes
a³ - b³ = (a - b)(a² + ab + b²)
For a³ ± b³, use SOAP: Same sign, Opposite sign, Always Positive
a³ + b³ = (a + b)(a² - ab + b²)
Multiplying Polynomials with More Than Two Terms
Distribute each term in the first polynomial:
= x(x² + 3x - 1) + 2(x² + 3x - 1)
= x³ + 3x² - x + 2x² + 6x - 2
= x³ + 5x² + 5x - 2
= x²(x - 1) + x(x - 1) + 1(x - 1)
= x³ - x² + x² - x + x - 1
= x³ - 1 (difference of cubes!)
Polynomial Vocabulary
| Term | Definition | Example |
|---|---|---|
| Monomial | 1 term | 5x² |
| Binomial | 2 terms | x + 3 |
| Trinomial | 3 terms | x² + 2x + 1 |
| Degree | Highest exponent | x³ + x has degree 3 |
| Leading term | Term with highest degree | In 2x³ + x, it's 2x³ |
| Leading coefficient | Coefficient of leading term | In 2x³ + x, it's 2 |
| Constant term | Term with no variable | In x² + 5, it's 5 |
Polynomial Division (Optional Advanced Topic)
You can divide polynomials using long division, similar to numerical long division.
Step 1: x² ÷ x = x (first term of quotient)
Step 2: x(x + 2) = x² + 2x
Step 3: (x² + 5x + 6) - (x² + 2x) = 3x + 6
Step 4: 3x ÷ x = 3 (second term of quotient)
Step 5: 3(x + 2) = 3x + 6
Step 6: (3x + 6) - (3x + 6) = 0 (no remainder)
Answer: (x² + 5x + 6) ÷ (x + 2) = x + 3
Check: (x + 2)(x + 3) = x² + 5x + 6 ✓
7. Factoring
What is Factoring, Really?
You already know how to multiply: take (x + 3)(x + 5) and expand it into x² + 8x + 15. Factoring is the reverse. You start with x² + 8x + 15 and figure out that it came from (x + 3)(x + 5).
Think of it like this: multiplication is baking a cake (mixing ingredients together). Factoring is looking at a finished cake and figuring out the recipe. You are "un-multiplying."
Factoring is how you solve equations. If you need to solve x² + 8x + 15 = 0, there is no way to "undo" the x² directly. But if you factor it into (x + 3)(x + 5) = 0, then you know either x + 3 = 0 or x + 5 = 0, giving you x = -3 or x = -5. That is the entire point: turn a hard problem into two easy ones.
First, Understand FOIL (So You Can Reverse It)
Before you can undo multiplication, you need to truly understand how multiplication works. FOIL stands for First, Outer, Inner, Last -- it is a way to multiply two binomials.
This is the key insight for factoring: the middle coefficient is the SUM of two numbers, and the last term is the PRODUCT of those same two numbers. When you factor, you are hunting for those two numbers.
The Area Model -- A Visual Way to See It
There is a beautiful visual way to understand what FOIL is really doing. Think of multiplying (x + 3)(x + 5) as finding the area of a rectangle with sides (x + 3) and (x + 5):
Factoring Strategy -- Follow This Every Time
- Step 1 -- GCF First: Always check if all terms share a common factor. If they do, pull it out. This is the single most important step and the one most people skip.
- Step 2 -- Count the terms:
- 2 terms? Check for difference of squares: a² - b² = (a + b)(a - b)
- 3 terms? Find two numbers that multiply to give the last term and add to give the middle. This is the core technique.
- 4 terms? Try factoring by grouping (pair them up).
- Step 3 -- Always verify: Multiply your answer back out. If it does not match the original, something went wrong.
Technique 1: Greatest Common Factor (GCF)
The GCF is the biggest thing that divides evenly into every term. Always do this first -- it makes everything else simpler.
How to find the GCF: look at the numbers (what is the biggest number that divides all of them?) and the variables (what is the lowest power of each variable that appears in every term?).
Factor: 6x² + 9x
Numbers: GCF of 6 and 9 is 3.
Variables: both terms have at least one x, so the GCF includes x.
GCF = 3x. Divide each term by 3x:
6x² / 3x = 2x and 9x / 3x = 3
Answer: 3x(2x + 3)
Verify: 3x * 2x = 6x², and 3x * 3 = 9x. That is 6x² + 9x. Correct.
Factor: 12x³y² - 8x²y³ + 4xy
Numbers: GCF of 12, 8, 4 is 4.
Variables: smallest power of x across all terms is x¹, smallest power of y is y¹.
GCF = 4xy. Divide each term:
12x³y² / 4xy = 3x²y, 8x²y³ / 4xy = 2xy², 4xy / 4xy = 1
Answer: 4xy(3x²y - 2xy² + 1)
Technique 2: Factoring Trinomials (x² + bx + c)
This is the most common type. You have a trinomial where the x² has no coefficient (or a coefficient of 1). You need to find two numbers p and q where:
Rule: p × q = c (they multiply to the last number)
p + q = b (they add to the middle number)
This is like a number puzzle: "I'm thinking of two numbers. They multiply to give 12, and they add to give 7. What are they?" The answer is 3 and 4.
Factor: x² + 7x + 12
We need two numbers that multiply to 12 and add to 7.
List the pairs that multiply to 12:
Factor: x² - 2x - 15
Need two numbers that multiply to -15 and add to -2.
Since the product is negative, one number must be positive and one must be negative. Since the sum is negative, the negative number must be bigger.
The signs in x² + bx + c tell you a lot about p and q:
- c is positive, b is positive: both p and q are positive. (e.g., x² + 7x + 12 = (x+3)(x+4))
- c is positive, b is negative: both p and q are negative. (e.g., x² - 7x + 12 = (x-3)(x-4))
- c is negative: one is positive, one is negative. The bigger one has the same sign as b. (e.g., x² - 2x - 15 = (x+3)(x-5))
Factor: x² - 9x + 20
c = +20 (positive) and b = -9 (negative), so both numbers are negative.
Need: multiply to 20, add to -9. Pairs: (-4)(-5) = 20 and -4 + -5 = -9.
Answer: (x - 4)(x - 5)
Technique 3: Factoring Harder Trinomials (ax² + bx + c, where a ≠ 1)
When the x² term has a coefficient other than 1 (like 2x² or 3x²), the basic "find two numbers" method needs a twist. Use the AC method:
1. Multiply a × c (the first and last coefficients)
2. Find two numbers that multiply to (a × c) and add to b
3. Rewrite the middle term using those two numbers
4. Factor by grouping
Factor: 2x² + 7x + 3
Factor: 3x² - 10x + 8
a × c = 3 × 8 = 24. Need two numbers that multiply to 24 and add to -10.
Since product is positive and sum is negative, both numbers are negative: (-4)(-6) = 24 and -4 + (-6) = -10.
Rewrite: 3x² - 4x - 6x + 8
Group: x(3x - 4) - 2(3x - 4) = (3x - 4)(x - 2)
Technique 4: Difference of Squares
This is one of the cleanest patterns in all of algebra. Whenever you see something squared minus something else squared, it factors instantly:
Why does this work? Think about it backwards. If you expand (a + b)(a - b) using FOIL:
Factor: x² - 49
Is this "something² minus something²"? Yes: x² = (x)² and 49 = (7)².
Answer: (x + 7)(x - 7)
Verify: (x + 7)(x - 7) = x² - 7x + 7x - 49 = x² - 49. Correct.
Factor: 4x² - 25
4x² = (2x)² and 25 = (5)². So a = 2x, b = 5.
Answer: (2x + 5)(2x - 5)
Factor: x&sup4; - 16
x&sup4; = (x²)² and 16 = (4)². So: (x² + 4)(x² - 4).
But wait -- x² - 4 is also a difference of squares! Factor it again:
Answer: (x² + 4)(x + 2)(x - 2)
(x² + 4 cannot be factored further because it is a sum of squares.)
Technique 5: Perfect Square Trinomials
Sometimes a trinomial is a perfect square in disguise. If the first and last terms are perfect squares, check if the middle term is twice the product of their square roots:
a² - 2ab + b² = (a - b)²
Factor: 4x² - 20x + 25
4x² = (2x)², and 25 = (5)². Is the middle 2(2x)(5) = 20x? Yes.
Answer: (2x - 5)²
Technique 6: Factoring by Grouping (4 Terms)
When you have 4 terms, try splitting them into two groups of 2, factor each group separately, and see if a common factor emerges.
Factor: x³ + 3x² + 2x + 6
- a² + b² does NOT factor. x² + 9 cannot be factored. Only a² minus b² factors. This is the most common mistake.
- Forgetting the GCF. Always check for a common factor first.
2x² + 4x + 2looks hard until you pull out the 2:2(x² + 2x + 1) = 2(x + 1)². - Sign errors in the AC method. When you split the middle term, make sure the signs are right. Always multiply back out to verify.
- Stopping too early. After factoring once, check if you can factor further.
x&sup4; - 16 = (x² + 4)(x² - 4)is not fully factored -- the second piece factors again into(x+2)(x-2).
Every factoring problem in algebra uses one or more of these techniques:
- GCF: Pull out the biggest common factor. Always first.
- Trinomial (a=1): Find two numbers that multiply to c and add to b.
- Trinomial (a≠1): AC method -- multiply a×c, find the pair, rewrite middle term, group.
- Difference of squares: a² - b² = (a+b)(a-b).
- Perfect square: a² ± 2ab + b² = (a ± b)².
- Grouping: Split 4 terms into 2 pairs, factor each, pull out common binomial.
If you can do these 6 techniques, you can factor anything that shows up in algebra, calculus, or a technical interview. Practice each one until it is automatic.
8. Quadratic Equations
Standard Form
A quadratic equation is any equation where the highest power of the variable is 2. The graph of a quadratic function is a parabola -- a U-shaped curve that opens up (if a > 0) or down (if a < 0).
Method 1: Solving by Factoring
If you can factor the quadratic, set each factor equal to zero and solve.
Solve: x² + 5x + 6 = 0
Step 1: Factor: (x + 2)(x + 3) = 0
Step 2: Set each factor to zero:
x + 2 = 0 → x = -2
x + 3 = 0 → x = -3
Solutions: x = -2 or x = -3
Solve: 2x² + 7x + 3 = 0
Step 1: Find two numbers that multiply to 2 * 3 = 6 and add to 7. That's 1 and 6.
Step 2: Rewrite the middle term: 2x² + x + 6x + 3 = 0
Step 3: Factor by grouping: x(2x + 1) + 3(2x + 1) = 0
Step 4: Factor out (2x + 1): (2x + 1)(x + 3) = 0
Solutions: x = -1/2 or x = -3
Method 2: The Quadratic Formula
Factoring is great when it works, but sometimes the numbers are ugly and you cannot find the pair. The quadratic formula is the guaranteed method -- it works for every quadratic equation, always. It is the universal key.
For the equation ax² + bx + c = 0, just plug in the values of a, b, and c. That is literally all you do.
What Each Part of the Formula Means
This formula looks scary but every piece has a job. Let's break it down:
The quadratic formula is what you get when you "complete the square" on the general equation ax² + bx + c = 0 (instead of a specific one). Someone did the algebra once, for the general case, and got this formula. Now you never have to complete the square again -- just plug in a, b, c.
Think of it like a function in programming. Instead of writing the same logic every time, someone wrote a reusable function: solve(a, b, c) that returns the answer for any quadratic.
- Always works -- even when factoring is impossible
- Use it when the numbers are ugly (fractions, large numbers, no obvious factor pairs)
- Use it when you need exact answers with square roots (not just integer solutions)
- Real-world uses: projectile motion (when does the ball hit the ground?), break-even analysis (when does profit = cost?), optimization problems, physics simulations
Solve: x² - 4x - 5 = 0
Here a = 1, b = -4, c = -5.
x = (-(-4) ± √((-4)² - 4(1)(-5))) / 2(1)
x = (4 ± √(16 + 20)) / 2
x = (4 ± √36) / 2
x = (4 ± 6) / 2
Two solutions:
x = (4 + 6) / 2 = 10 / 2 = 5
x = (4 - 6) / 2 = -2 / 2 = -1
Solutions: x = 5 or x = -1
Solve: 2x² + 3x - 4 = 0
Here a = 2, b = 3, c = -4.
x = (-3 ± √(9 + 32)) / 4
x = (-3 ± √41) / 4
Since √41 ≈ 6.403:
x ≈ (-3 + 6.403) / 4 ≈ 0.851
x ≈ (-3 - 6.403) / 4 ≈ -2.351
Solutions: x = (-3 + √41) / 4 or x = (-3 - √41) / 4
The Discriminant -- Peek at the Answer Before Solving
The expression under the square root, b² - 4ac, is called the discriminant. It is like a preview -- before you even solve the equation, the discriminant tells you how many answers you will get.
Why? Because the ± in the formula gives you two answers: one with + and one with -. But if the thing inside the square root is zero, both answers are the same. And if it is negative, you cannot take the square root at all (no real answer).
| Discriminant | Number of Solutions | What It Means |
|---|---|---|
| b² - 4ac > 0 | Two real solutions | Parabola crosses x-axis twice |
| b² - 4ac = 0 | One real solution | Parabola just touches x-axis (the vertex lands exactly on it) |
| b² - 4ac < 0 | No real solutions | Parabola floats above (or below) the x-axis and never touches it |
Method 3: Completing the Square
This technique rewrites the quadratic by creating a perfect square trinomial. It's the method used to derive the quadratic formula itself.
Solve: x² + 6x + 2 = 0
Step 1: Move the constant: x² + 6x = -2
Step 2: Take half of 6, which is 3, and square it to get 9. Add 9 to both sides:
x² + 6x + 9 = -2 + 9
(x + 3)² = 7
Step 3: Take the square root of both sides:
x + 3 = ±√7
Step 4: Subtract 3:
x = -3 ± √7
Solutions: x = -3 + √7 ≈ -0.354 and x = -3 - √7 ≈ -5.646
In the quadratic formula, the entire numerator (-b ± √(b² - 4ac)) is divided by 2a, not just the square root part. Many students write x = -b ± √(b² - 4ac) / 2a, which would incorrectly divide only the √ part by 2a. Use parentheses to keep it clear.
Quadratic equations show up in physics simulations (projectile motion follows a parabola), game development (collision detection), and algorithm analysis. Some recurrence relations that describe algorithm runtimes lead to quadratic equations. The quadratic formula itself is a great example of how a general formula can solve a whole class of problems -- the same mindset behind writing reusable functions in code.
9. Exponent Rules (Advanced)
Exponents are shorthand for repeated multiplication. x³ means x * x * x. The base is x and the exponent (or power) is 3.
The Core Rules
| Rule | Formula | Example |
|---|---|---|
| Product Rule | xa * xb = xa+b | x³ * x² = x5 |
| Quotient Rule | xa / xb = xa-b | x5 / x² = x³ |
| Power Rule | (xa)b = xab | (x³)² = x6 |
| Product to Power | (xy)a = xa * ya | (2x)³ = 8x³ |
| Quotient to Power | (x/y)a = xa / ya | (x/3)² = x²/9 |
Zero and Negative Exponents
x-n = 1 / xn
Why does x0 = 1? Think of it this way: x³ / x³ = 1 (anything divided by itself is 1). But by the quotient rule, x³ / x³ = x3-3 = x0. So x0 must equal 1.
Why does x-n = 1/xn? Same logic: x² / x5 = 1/x³ (dividing out). But by the quotient rule, x² / x5 = x2-5 = x-3. So x-3 = 1/x³.
Simplify: 2-3
2-3 = 1 / 2³ = 1/8
Fractional Exponents (Roots)
xm/n = (n√x)m = n√(xm)
Simplify: 82/3
Step 1: 81/3 = 3√8 = 2
Step 2: 2² = 4
Answer: 82/3 = 4
Scientific Notation
Scientific notation expresses very large or very small numbers using powers of 10:
4,500,000 = 4.5 × 106 (moved the decimal 6 places left)
0.00032 = 3.2 × 10-4 (moved the decimal 4 places right)
The product rule (xa * xb = xa+b) only works when the bases are the same. You can simplify x³ * x² = x5, but you CANNOT simplify x³ * y² this way. Also, don't confuse the product rule (add exponents) with the power rule (multiply exponents).
Complete Laws of Exponents Summary
Here are all the exponent laws in one place. Understanding why each works will help you remember them.
Law 2: x0 = 1 — Any non-zero number to the zero power is 1
Law 3: x-n = 1/xn — Negative exponent means "reciprocal"
Law 4: xm × xn = xm+n — Multiplying same bases: ADD exponents
Law 5: xm ÷ xn = xm-n — Dividing same bases: SUBTRACT exponents
Law 6: (xm)n = xm×n — Power of a power: MULTIPLY exponents
Law 7: (xy)n = xn × yn — Power of a product
Law 8: (x/y)n = xn / yn — Power of a quotient
Law 9: xm/n = n√(xm) = (n√x)m — Fractional exponents = roots
Deep Dive: Why These Laws Work
Why x0 = 1?
Consider: x3 ÷ x3 = 1 (anything divided by itself is 1)
But using Law 5: x3 ÷ x3 = x3-3 = x0
Therefore: x0 = 1
Why x-n = 1/xn?
Consider: x2 ÷ x5 = x × x / (x × x × x × x × x) = 1/(x × x × x) = 1/x3
But using Law 5: x2 ÷ x5 = x2-5 = x-3
Therefore: x-3 = 1/x3
Practice Problems: Simplifying Exponents
Simplify: 24 × 23
= 24+3 = 27 = 128
Simplify: 58 ÷ 55
= 58-5 = 53 = 125
Simplify: (32)4
= 32×4 = 38 = 6561
Simplify: 4-2
= 1/42 = 1/16 = 0.0625
Simplify: 272/3
= (3√27)2 = 32 = 9
Or: 3√(272) = 3√729 = 9
Simplify: (x3y2)4
= (x3)4 × (y2)4
= x12 × y8 = x12y8
Simplify: (2x3)2 × (3x-2)
= 22 × x6 × 3 × x-2
= 4 × 3 × x6-2
= 12x4
Moving Between Numerator and Denominator
A negative exponent in the numerator becomes positive when moved to the denominator, and vice versa:
1/x-n = xn
Moving rule: When you move a term across the fraction bar, flip the sign of its exponent.
Write without negative exponents: x-3y2 / z-1
= y2z / x3
(x-3 moves to denominator as x3, z-1 moves to numerator as z)
Common Exponent Patterns
| Expression | Result | Pattern |
|---|---|---|
| 20 | 1 | Anything to the 0 is 1 |
| (-3)2 | 9 | Negative base, even exponent = positive |
| (-3)3 | -27 | Negative base, odd exponent = negative |
| -32 | -9 | This is -(32), NOT (-3)2 |
| 05 | 0 | 0 to any positive power is 0 |
| 00 | undefined | Mathematically ambiguous |
-32 ≠ (-3)2
-32 means -(32) = -9 (the negative is not part of the base)
(-3)2 means (-3) × (-3) = 9 (the negative IS part of the base)
This distinction is crucial in programming too: -3**2 vs (-3)**2
Exponents are fundamental to understanding big O notation. When we say an algorithm is O(n²) or O(2n), we're using exponents to describe how fast the runtime grows. Scientific notation is essentially how floating-point numbers are stored in computer memory -- as a mantissa times a power of 2. Understanding exponent rules helps you simplify algorithm complexity expressions like O(n² * n³) = O(n5).
Big-O Simplification Mindset
When you see a complex expression and need to find the Big-O, your brain might want to simplify it perfectly. Stop. Big-O is approximation math, not exact math. You only need to find the biggest term.
1. Drop constants: O(5n²) = O(n²). Constants don't affect growth rate.
2. Drop lower-order terms: O(n² + n) = O(n²). Smaller terms become irrelevant as n grows.
3. The highest exponent wins: In n4 + n² + n, the n4 dominates. O(n4).
Step 1: Expand using distribution
= 3n × 40 + 3n × n³/2
= 120n + 3n4/2
Step 2: Find the Big-O
We have two terms: 120n and 3n4/2
Which has the higher exponent?
- 120n = 120n1 → exponent is 1
- 3n4/2 = (3/2)n4 → exponent is 4
Step 3: Apply the rules
Drop the lower term (120n): gone.
Drop the constant (3/2): gone.
What remains: O(n4)
| Expression | Expand | Dominant Term | Big-O |
|---|---|---|---|
| 5n + 2 | (already expanded) | 5n | O(n) |
| n² + 100n + 500 | (already expanded) | n² | O(n²) |
| n(n+1)/2 | n²/2 + n/2 | n²/2 | O(n²) |
| 3n4 + n² + 7 | (already expanded) | 3n4 | O(n4) |
| 2n + n100 | (already expanded) | 2n | O(2n) |
Note: Exponentials (2n) always beat polynomials (n100), no matter how big the exponent. 2n grows faster than n1000000 for large enough n.
Your brain wants to simplify 3n4/2 into a clean number. For Big-O, you don't need to. The moment you see n4, you're done. You don't care about the 3 or the /2. Engineers ask "what dominates when n is huge?" not "is this algebraically pristine?"
Think of it like this: if n = 1,000,000 then n4 = 1,000,000,000,000,000,000,000,000. The 120n term adds 120,000,000 to that. That's 0.000000000000012% of the total. It literally doesn't matter.
Big-O lets you skip simplification. But when solving equations, computing actual values, or proving formulas, you need exact algebra. The skill is knowing which mode you're in:
- Big-O mode: Find the dominant term, drop everything else. Approximation is the goal.
- Exact mode: Every step must be precise. Fractions, signs, and constants all matter.
Most algorithm analysis lives in Big-O mode. Most debugging and implementation lives in exact mode.
10. Logarithms
The One Sentence You Need
Forget the formal definition for a second. Here's the intuition that matters:
That's it. That's the whole idea. Everything else follows from this.
Start with 32. Keep halving until you hit 1. Count the steps:
32 → 16 → 8 → 4 → 2 → 1 5 steps
So log2(32) = 5. Done.
Try 1,000,000:
1,000,000 → 500,000 → 250,000 → 125,000 → 62,500 → 31,250 → 15,625 → ~7,813 → ~3,906 → ~1,953 → ~977 → ~488 → ~244 → ~122 → ~61 → ~31 → ~15 → ~8 → ~4 → ~2 → 1
About 20 steps. log2(1,000,000) ≈ 20. A million items reduced to 20 steps by halving!
The game "20 Questions" is literally binary search. With each yes/no question, you eliminate half the possibilities.
- After 1 question: 500,000 possibilities remain
- After 2 questions: 250,000
- After 10 questions: ~1,000
- After 20 questions: ~1
With 20 yes/no questions, you can find any item among a million. That's the power of log2(n). It measures how many times you split the problem in half.
The Formal Definition
Now that you have the intuition, here's the math: a logarithm answers "What power do I raise this base to in order to get this number?" It is the inverse of exponentiation.
"log base b of x equals y" means "b raised to the y equals x"
Exponentiation: 25 = 32 (I know the power, what's the result?)
Logarithm: log2(32) = 5 (I know the result, what's the power?)
log2(8) = ?
Ask: "2 to what power gives 8?" 2³ = 8, so log2(8) = 3
Or think: "How many halvings? 8 → 4 → 2 → 1 = 3 halvings"
log10(1000) = ?
Ask: "10 to what power gives 1000?" 10³ = 1000, so log10(1000) = 3
Or think: "How many digits minus 1?" 1000 has 4 digits, so log10(1000) = 3
log5(25) = ?
Ask: "5 to what power gives 25?" 5² = 25, so log5(25) = 2
The log2 Table You Must Memorize
These values come up constantly in CS. Don't memorize them by rote -- see the pattern:
| n | log2(n) | Why (halving) | Where You See This |
|---|---|---|---|
| 1 | 0 | Already at 1, zero halvings | Base case |
| 2 | 1 | 2 → 1 (1 halving) | One comparison |
| 4 | 2 | 4 → 2 → 1 | 2-bit number |
| 8 | 3 | 8 → 4 → 2 → 1 | 3-bit number, byte = 2³ |
| 16 | 4 | 4 halvings | Hex digit (0-F) |
| 32 | 5 | 5 halvings | 32-bit int |
| 64 | 6 | 6 halvings | 64-bit systems |
| 128 | 7 | 7 halvings | ASCII character set |
| 256 | 8 | 8 halvings | 1 byte = 2&sup8; = 256 values |
| 1,024 | 10 | 10 halvings | 1 KB (kibibyte) |
| 1,048,576 | 20 | 20 halvings | 1 MB |
| ~1 billion | 30 | 30 halvings | 1 GB |
| ~4.3 billion | 32 | 32 halvings | IPv4 addresses, 32-bit max |
log2 grows absurdly slowly. You need to DOUBLE n just to add 1 to log2(n). Going from 1 million to 1 billion (1000x more data) only adds 10 more steps. This is why O(log n) algorithms are so fast -- they barely notice when you throw more data at them.
log2 in Actual Algorithms
You have a sorted array of 1,000 elements. How many comparisons does binary search need?
# Array of 1000 sorted elements
# Step 1: Check middle (index 500). Too high? Search left half (0-499).
# Step 2: Check middle of left half (index 250). Too low? Search right (251-499).
# Step 3: Check middle (index 375). Keep halving...
# ...
# After ~10 steps: found it!
#
# Why 10? Because log₂(1000) ≈ 10.
# Each step halves the search space: 1000 → 500 → 250 → 125 → 63 → 32 → 16 → 8 → 4 → 2 → 1
Linear search would check up to 1,000 elements. Binary search: 10. That's the power of log n.
A balanced binary tree with n nodes has height log2(n).
Why? Each level doubles the number of nodes: 1, 2, 4, 8, 16, ...
So a tree with 1 million nodes is only about 20 levels deep.
That's why tree lookups (BST, AVL, Red-Black) are O(log n) -- you descend at most log2(n) levels.
The number of bits needed to represent n in binary is ⌊log2(n)⌋ + 1.
Why? Each bit doubles the range: 1 bit → 2 values, 2 bits → 4 values, k bits → 2k values.
To represent 1000: log2(1000) ≈ 9.97, so you need 10 bits. Check: 210 = 1024 ≥ 1000.
Common and Natural Logs
- log2(x) = "CS log" -- the one you'll use 90% of the time. How many halvings / doublings.
- log10(x) = "common log" -- roughly counts the number of digits. log10(1000) = 3 and 1000 has 4 digits.
- ln(x) = loge(x) = "natural log" -- where e ≈ 2.71828. Shows up in calculus, probability, and continuous growth/decay.
In Big-O notation, we just write O(log n) without specifying the base. Why?
The change of base formula: log2(n) = log10(n) / log10(2) ≈ 3.32 × log10(n)
So log2(n) and log10(n) differ only by a constant factor (3.32), and Big-O ignores constants. All log bases are equivalent up to a constant, so O(log2 n) = O(log10 n) = O(ln n) = O(log n).
Logarithm Rules
| Rule | Formula | In Words |
|---|---|---|
| Product Rule | log(ab) = log(a) + log(b) | Log of a product = sum of logs |
| Quotient Rule | log(a/b) = log(a) - log(b) | Log of a quotient = difference of logs |
| Power Rule | log(an) = n × log(a) | Exponent comes down as multiplier |
| Change of Base | logb(x) = log(x) / log(b) | Convert between bases |
| Identity | logb(b) = 1 | Any base logged to itself is 1 |
| Identity | logb(1) = 0 | Log of 1 is always 0 |
| Inverse | blogb(x) = x | Exponent and log cancel out |
Expand: log(x²y/z)
Step 1: Apply quotient rule: log(x²y) - log(z)
Step 2: Apply product rule: log(x²) + log(y) - log(z)
Step 3: Apply power rule: 2log(x) + log(y) - log(z)
Answer: 2log(x) + log(y) - log(z)
Think about it in binary (base 2):
log2(8 × 4) = log2(32) = 5
log2(8) + log2(4) = 3 + 2 = 5 ✓
Why does this work? Multiplying by 8 means "shift left 3 bits" and multiplying by 4 means "shift left 2 bits." Total shift: 3 + 2 = 5. Logs count the shifts!
Solving Exponential Equations with Logs
When the variable is in the exponent, use logarithms to bring it down.
Solve: 2x = 32
Method 1 (recognition): 25 = 32, so x = 5.
Method 2 (halving intuition): 32 → 16 → 8 → 4 → 2 → 1. That's 5 halvings, so x = 5.
Method 3 (logs): Take log of both sides:
log(2x) = log(32)
x × log(2) = log(32)
x = log(32) / log(2) = 5
Solve: 3x = 20
Take log of both sides: x × log(3) = log(20)
x = log(20) / log(3) = 1.3010 / 0.4771 ≈ 2.727
Your database grows 5% per month. When will it double?
You need: 1.05x = 2
x = log(2) / log(1.05) = 0.3010 / 0.02119 ≈ 14.2 months
Rule of 72 shortcut: 72 / growth rate ≈ doubling time. So 72 / 5 ≈ 14.4 months. Close enough!
Thinking in Logs: The Cheat Sheet
When you see these patterns, think "logarithm":
| You See... | Think... | Example |
|---|---|---|
| "Halving repeatedly" | log2(n) steps | Binary search, divide & conquer |
| "How many digits?" | log10(n) + 1 digits | Number of digits in n |
| "How many bits?" | log2(n) + 1 bits | Binary representation |
| "Tree height" | log2(n) levels | Balanced BST, heap |
| "Splitting into k parts" | logk(n) levels | B-tree (k-way split) |
| "When does it double?" | Solve with logs | Growth rate analysis |
| "Undoing an exponent" | Apply log to both sides | Solving 2x = 1024 |
log(a + b) is NOT log(a) + log(b). There is no log rule for sums. The product rule says log(a × b) = log(a) + log(b), but this only works for multiplication, not addition. This is the #1 algebra error students make with logs.
log(0) is undefined. You can't raise any positive number to a power and get 0. And log of negatives doesn't exist in real numbers either.
If you can answer these without a calculator, you understand log2:
- log2(64) = ? (6 -- because 2&sup6; = 64)
- log2(1) = ? (0 -- because 2&sup0; = 1)
- log2(1024) = ? (10 -- because 210 = 1024)
- How many binary search steps for 1 billion items? (~30)
- Height of a balanced BST with 1 million nodes? (~20)
11. Sequences and Series
A sequence is an ordered list of numbers following a pattern. A series is the sum of a sequence. Understanding these is essential for algorithm analysis -- every time you analyze a loop or recursion, you're working with sequences and sums.
What is a Sequence? (The Basics)
Think of a sequence as a numbered list where each position has exactly one value:
Natural numbers: 1, 2, 3, 4, 5, 6, ... (position 1 = 1, position 2 = 2, etc.)
Even numbers: 2, 4, 6, 8, 10, ... (position n = 2n)
Squares: 1, 4, 9, 16, 25, ... (position n = n²)
Powers of 2: 1, 2, 4, 8, 16, 32, ... (position n = 2^(n-1))
We write an to mean "the nth term of the sequence." So if our sequence is 2, 4, 6, 8, 10, ... then:
- a₁ = 2 (first term)
- a₂ = 4 (second term)
- a₃ = 6 (third term)
- an = 2n (formula for any term)
What is a Series? (Sum of a Sequence)
A series is what you get when you add up the terms of a sequence:
Sequence: 1, 2, 3, 4, 5 (a list)
Series: 1 + 2 + 3 + 4 + 5 = 15 (a sum)
Sequence: 1, 2, 4, 8 (a list)
Series: 1 + 2 + 4 + 8 = 15 (a sum)
When you write a loop, you're essentially generating a sequence. When you count how many times that loop runs, you're computing a series. For example:
# This loop generates the sequence 0, 1, 2, 3, 4
for i in range(5):
print(i)
# How many iterations? That's 5 (the sum of "1" five times)
# If you nested loops, you'd need series formulas to count total iterations
Arithmetic Sequences
In an arithmetic sequence, each term differs from the previous by a constant amount called the common difference (d).
Subtract any term from the next. If you always get the same number, it's arithmetic!
3, 7, 11, 15, 19, ...
7 - 3 = 4, 11 - 7 = 4, 15 - 11 = 4, 19 - 15 = 4 → d = 4 ✓
10, 7, 4, 1, -2, ...
7 - 10 = -3, 4 - 7 = -3, 1 - 4 = -3 → d = -3 ✓ (can be negative!)
Where:
• a₁ = first term
• d = common difference
• n = term number
• aₙ = the nth term
Sequence: 3, 7, 11, 15, 19, ...
Step 1: Identify a₁ (first term) = 3
Step 2: Find d (common difference) = 7 - 3 = 4
Step 3: Write the formula: aₙ = 3 + (n - 1)(4)
Step 4: Simplify: aₙ = 3 + 4n - 4 = 4n - 1
Find the 10th term:
a₁₀ = 4(10) - 1 = 40 - 1 = 39
Find the 100th term:
a₁₀₀ = 4(100) - 1 = 400 - 1 = 399
Notice: with the formula, jumping to the 100th term is instant -- no need to calculate all 99 previous terms!
The formula uses (n - 1), NOT n. Why? Because the first term doesn't "add" any d yet:
- a₁ = a₁ + (1-1)d = a₁ + 0d = a₁ ✓
- a₂ = a₁ + (2-1)d = a₁ + 1d ✓
- a₃ = a₁ + (3-1)d = a₁ + 2d ✓
To get from term 1 to term n, you add d exactly (n-1) times.
Arithmetic Series (Sum of Arithmetic Sequence)
The sum of the first n terms of an arithmetic sequence:
Or equivalently: Sₙ = n/2 × (2a₁ + (n-1)d)
Why does Sₙ = n/2 × (first + last) work?
Take 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10:
Pair the first with the last: 1 + 10 = 11
Pair the second with second-to-last: 2 + 9 = 11
Pair 3 + 8 = 11, 4 + 7 = 11, 5 + 6 = 11
We have 5 pairs (that's n/2 = 10/2), each summing to 11 (that's first + last).
Total: 5 × 11 = 55
Find: 1 + 2 + 3 + ... + 100
This is an arithmetic sequence with a₁ = 1, aₙ = 100, n = 100
S₁₀₀ = 100/2 × (1 + 100) = 50 × 101 = 5,050
Legend has it that Gauss solved this as a child by noticing 1+100 = 2+99 = 3+98 = ... = 101, and there are 50 such pairs.
Find: 1 + 3 + 5 + 7 + 9 (first 5 odd numbers)
a₁ = 1, d = 2, n = 5, a₅ = 1 + (5-1)(2) = 9
S₅ = 5/2 × (1 + 9) = 2.5 × 10 = 25
Fun fact: The sum of the first n odd numbers is always n². Try it: 1+3=4=2², 1+3+5=9=3², 1+3+5+7=16=4²!
The sum 1 + 2 + 3 + ... + n = n(n+1)/2 appears constantly in algorithm analysis:
for i in range(n):
for j in range(i): # Inner loop runs 0, 1, 2, ..., n-1 times
print(i, j)
# Total iterations: 0 + 1 + 2 + ... + (n-1) = n(n-1)/2 = O(n²)
This is why nested triangular loops are O(n²), not O(n)!
Geometric Sequences
In a geometric sequence, each term is multiplied by a constant called the common ratio (r).
Divide any term by the previous term. If you always get the same number, it's geometric!
2, 6, 18, 54, 162, ...
6/2 = 3, 18/6 = 3, 54/18 = 3, 162/54 = 3 → r = 3 ✓
100, 50, 25, 12.5, ...
50/100 = 0.5, 25/50 = 0.5, 12.5/25 = 0.5 → r = 0.5 ✓ (can be a fraction!)
Arithmetic vs Geometric:
Arithmetic: add the same amount (+d) → linear growth
Geometric: multiply by the same factor (×r) → exponential growth
Where:
• a₁ = first term
• r = common ratio
• n = term number
Sequence: 2, 6, 18, 54, 162, ...
Step 1: Identify a₁ = 2
Step 2: Find r = 6/2 = 3
Step 3: Write the formula: aₙ = 2 × 3^(n-1)
Find the 8th term:
a₈ = 2 × 3^(8-1) = 2 × 3⁷ = 2 × 2187 = 4,374
Notice how fast geometric sequences grow! The 8th term is over 4,000 even though we started at 2.
You invest $1,000 at 10% annual interest. How much after 5 years?
Each year, you multiply by 1.10 (100% + 10% = 110% = 1.10)
a₁ = 1000, r = 1.10, n = 6 (year 0 through year 5)
a₆ = 1000 × 1.10⁵ = 1000 × 1.61051 = $1,610.51
A radioactive substance has a half-life of 1 hour. Starting with 800g, how much after 4 hours?
Each hour, you multiply by 0.5 (half remains)
a₁ = 800, r = 0.5, n = 5 (hour 0 through hour 4)
a₅ = 800 × 0.5⁴ = 800 × 0.0625 = 50g
Exponential Growth: Why It Destroys Everything
Humans are terrible at intuiting exponential growth. Our brains think linearly, so exponentials always surprise us. Let's fix that.
A king agrees to pay 1 grain of rice on square 1, 2 grains on square 2, 4 on square 3, doubling each square on a 64-square chessboard.
Square 1: 1 grain
Square 10: 512 grains (barely a pinch)
Square 20: 524,288 grains (a small bag)
Square 30: ~537 million grains (~13 tons of rice)
Square 40: ~550 billion grains (~13,000 tons)
Square 64: 263 = 9,223,372,036,854,775,808 grains
That's ~230 billion tons of rice -- more than all rice ever produced in human history.
This is why 2n algorithms are death sentences. The numbers look fine until they suddenly don't.
| n | log2(n) | n | n log n | n² | 2n | n! |
|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 0 | 1 | 2 | 1 |
| 5 | 2.3 | 5 | 12 | 25 | 32 | 120 |
| 10 | 3.3 | 10 | 33 | 100 | 1,024 | 3,628,800 |
| 20 | 4.3 | 20 | 86 | 400 | 1,048,576 | ~2.4 × 1018 |
| 30 | 4.9 | 30 | 147 | 900 | ~1 billion | ~2.7 × 1032 |
| 50 | 5.6 | 50 | 282 | 2,500 | ~1015 | ~3 × 1064 |
| 100 | 6.6 | 100 | 664 | 10,000 | ~1030 | ~9 × 10157 |
At n=100: log n is 7, n² is 10,000, but 2n has 30 digits. Your computer running 10 billion operations/second would need 1020 seconds for 2100 operations. The universe is only 4 × 1017 seconds old.
You're dealing with exponential growth when:
- Each step doubles the work: Recursive Fibonacci, brute-force subset enumeration
- You're trying "all combinations": Password cracking, the traveling salesman problem
- A function calls itself multiple times:
f(n) = f(n-1) + f(n-2)-- each call spawns two more
And exponential decay (good!) when:
- Each step halves the problem: Binary search, balanced tree operations
- Probability drops exponentially: Hash collision chains, retry backoff
- Cache hit rates: Working set usually follows an exponentially decaying access pattern
If something grows by x% each period, it doubles in roughly 72/x periods.
Examples:
- Data growing at 10% per month → doubles in ~7.2 months
- Users growing at 5% per week → doubles in ~14.4 weeks
- CPU speed doubling every 18 months (Moore's Law) → ~100% growth every 18 months
This is critical for capacity planning. If your server handles today's load fine but traffic doubles every 3 months, you have a 3-month runway, not years.
Geometric Series (Sum of Geometric Sequence)
Alternative form: Sₙ = a₁ × (rⁿ - 1)/(r - 1) (more intuitive when r > 1)
Infinite sum (|r| < 1): S∞ = a₁/(1 - r)
Find: 1 + 2 + 4 + 8 + 16 + 32 (sum of first 6 terms)
a₁ = 1, r = 2, n = 6
S₆ = 1 × (2⁶ - 1)/(2 - 1) = (64 - 1)/1 = 63
Shortcut for powers of 2: 1 + 2 + 4 + ... + 2^(n-1) = 2ⁿ - 1
So 1 + 2 + 4 + 8 + 16 + 32 = 2⁶ - 1 = 64 - 1 = 63 ✓
This pattern appears EVERYWHERE in computer science:
- Binary numbers: An 8-bit number ranges from 0 to 2⁸ - 1 = 255
- Max unsigned int (32-bit): 2³² - 1 = 4,294,967,295
- Perfect binary tree: A complete binary tree of height h has 2^(h+1) - 1 nodes
Here's why: a binary tree of height 3 has 1 + 2 + 4 + 8 = 15 = 2⁴ - 1 nodes.
The sum 1 + 2 + 4 + 8 + ... + 2^(n-1) = 2ⁿ - 1 explains:
- Binary numbers: An n-bit unsigned integer can represent values from 0 to 2ⁿ - 1
- Recursive branching: A binary tree of depth n has at most 2ⁿ - 1 nodes
- Naive Fibonacci: Each call spawns 2 more calls → exponential growth
# Total calls in a binary recursive tree of depth n:
# Level 0: 1 call
# Level 1: 2 calls
# Level 2: 4 calls
# ...
# Level n-1: 2^(n-1) calls
# Total: 1 + 2 + 4 + ... + 2^(n-1) = 2ⁿ - 1 = O(2ⁿ)
Find: 1 + 1/2 + 1/4 + 1/8 + 1/16 + ...
a₁ = 1, r = 1/2 (|r| < 1, so it converges)
S∞ = 1/(1 - 1/2) = 1/(1/2) = 2
No matter how many terms you add, you'll never exceed 2!
Summation Notation (Sigma Notation)
Sigma notation (Σ) is a compact way to write sums. Think of it as a for loop in math form:
Read it as: "Sum of f(i) as i goes from 1 to n"
The parts:
• i = the loop variable (called the "index")
• 1 = starting value (bottom of Σ)
• n = ending value (top of Σ)
• f(i) = what you're adding each iteration (the body)
Every summation is literally a for loop. Every for loop that accumulates a total is a summation.
# This Python code...
total = 0
for i in range(1, n+1):
total += f(i)
# ...is EXACTLY this math:
# Σᵢ₌₁ⁿ f(i)
Once you see this connection, summations stop being scary and start being familiar.
Σᵢ₌₁⁵ i = 1 + 2 + 3 + 4 + 5 = 15
Σᵢ₌₁⁴ i² = 1² + 2² + 3² + 4² = 1 + 4 + 9 + 16 = 30
Σᵢ₌₀³ 2ⁱ = 2⁰ + 2¹ + 2² + 2³ = 1 + 2 + 4 + 8 = 15
Σᵢ₌₁³ (2i + 1) = 3 + 5 + 7 = 15
Translating Loops to Summations (The Skill That Matters)
This is the real reason you learn summations: to analyze code complexity. Here's the systematic method:
for i in range(n): # i goes from 0 to n-1
do_something() # O(1) work each time
# Total work: Σᵢ₌₀ⁿ⁻¹ 1 = n
# Big-O: O(n)
for i in range(n): # i = 0, 1, 2, ..., n-1
for j in range(i): # inner runs 0, 1, 2, ..., n-1 times
do_something()
# Inner loop runs: 0 + 1 + 2 + 3 + ... + (n-1)
# As a summation: Σᵢ₌₀ⁿ⁻¹ i = n(n-1)/2
# Big-O: O(n²)
i = n
while i >= 1:
do_something() # O(1) work
i = i // 2 # halve each iteration
# Iterations: n, n/2, n/4, ..., 2, 1
# Number of iterations: log₂(n)
# Big-O: O(log n)
for i in range(n): # O(n) outer iterations
j = n
while j >= 1: # O(log n) inner iterations
do_something()
j = j // 2
# Total: Σᵢ₌₀ⁿ⁻¹ log(n) = n × log(n)
# Big-O: O(n log n)
for i in range(n): # outer: n iterations
for j in range(2**i): # inner: 1, 2, 4, 8, ... 2^(n-1) iterations
do_something()
# Total: Σᵢ₌₀ⁿ⁻¹ 2ⁱ = 2ⁿ - 1
# Big-O: O(2ⁿ)
| Loop Pattern | Summation | Result | Big-O |
|---|---|---|---|
| Simple loop (0 to n) | Σ 1 | n | O(n) |
| Nested, both 0 to n | ΣΣ 1 | n² | O(n²) |
| Triangular (j < i) | Σᵢ₌₀ⁿ i | n(n-1)/2 | O(n²) |
| Halving (i = i/2) | Σ 1 (log n times) | log n | O(log n) |
| Outer n × inner log n | Σ log n | n log n | O(n log n) |
| Doubling inner (2i) | Σ 2ⁱ | 2ⁿ - 1 | O(2ⁿ) |
Important Sum Formulas
These formulas appear constantly in algorithm analysis. Memorize them!
| Sum | Formula | CS Application |
|---|---|---|
| 1 + 2 + 3 + ... + n | n(n+1)/2 | Triangular nested loops → O(n²) |
| 1² + 2² + 3² + ... + n² | n(n+1)(2n+1)/6 | Some triple nested patterns |
| 1 + 2 + 4 + ... + 2^(n-1) | 2ⁿ - 1 | Binary trees, recursive branching → O(2ⁿ) |
| 1 + r + r² + ... + rⁿ | (rⁿ⁺¹ - 1)/(r - 1) | Geometric growth patterns |
| n + n/2 + n/4 + ... + 1 | ≈ 2n | Work in merge sort levels → O(n) |
| 1 + 1/2 + 1/3 + ... + 1/n | ≈ ln(n) | Harmonic series (QuickSort avg case) |
Harmonic Series
The harmonic series is: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
The harmonic series appears in:
- QuickSort expected comparisons: Average case involves harmonic sums
- Coupon collector problem: Expected tries to collect all n coupons ≈ n × Hₙ ≈ n ln(n)
- Hash table analysis: Expected probe count with linear probing
Connecting Sequences to Recursion
Many sequences are defined recursively -- each term depends on previous terms.
Fibonacci: F(n) = F(n-1) + F(n-2), with F(0) = 0, F(1) = 1
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Factorial as a sequence: n! = n × (n-1)!
1, 1, 2, 6, 24, 120, 720, ...
Powers of 2: a(n) = 2 × a(n-1), with a(0) = 1
1, 2, 4, 8, 16, 32, ...
The Fibonacci recurrence T(n) = T(n-1) + T(n-2) creates exponential calls because each call branches into two more. The number of calls follows the Fibonacci sequence itself, which grows like φⁿ (where φ ≈ 1.618). This is why fib(50) with naive recursion is impossibly slow, but dynamic programming solves it in O(n).
Sequences and series are the mathematical language of algorithm analysis:
- Counting loop iterations = summing an arithmetic series
- Recursive branching = geometric series
- Master Theorem = solving recurrence relations (recursive sequences)
- Amortized analysis = understanding how costs average over a sequence of operations
Every time you analyze code complexity, you're working with these formulas.
12. Practice Quiz
Test your understanding of the key algebra concepts covered on this page. Click an answer to check it.
1. Simplify: 3(2x - 4) + 5x
First distribute: 3(2x - 4) = 6x - 12. Then combine with +5x: 6x - 12 + 5x = 11x - 12. Remember to distribute the 3 to both terms inside the parentheses.
2. Solve for x: 5x - 3 = 2x + 12
Subtract 2x from both sides: 3x - 3 = 12. Add 3 to both sides: 3x = 15. Divide by 3: x = 5. Check: 5(5) - 3 = 22 and 2(5) + 12 = 22. Correct!
3. Factor completely: x² - 9
This is a difference of squares. x² - 9 = x² - 3² = (x + 3)(x - 3). Remember: a² - b² = (a + b)(a - b). Note that (x - 3)² would expand to x² - 6x + 9, which is different.
4. What is the value of the discriminant for 2x² + 3x + 5 = 0, and what does it tell you?
The discriminant is b² - 4ac = 3² - 4(2)(5) = 9 - 40 = -31. Since the discriminant is negative, the square root of a negative number is not a real number, so this equation has no real solutions. The parabola doesn't cross the x-axis.
5. Simplify using log rules: log2(8) + log2(4)
Method 1 (evaluate each): log2(8) = 3 (since 2³ = 8) and log2(4) = 2 (since 2² = 4). So 3 + 2 = 5.
Method 2 (product rule): log2(8) + log2(4) = log2(8 * 4) = log2(32) = 5 (since 25 = 32).
Note: log2(12) is wrong -- the product rule says log(a) + log(b) = log(a*b), NOT log(a+b).