Project 3: Algebraic Equation Solver
A self-contained deep-dive from the canonical Math from Foundations to Machine Learning curriculum.
- Difficulty: Beginner
- Time: 6-10 hours
- Language: Python (alternatives: JavaScript, C++)
- Prerequisites: Projects 1-2
- Source:
HS P01
What You Will Build
Build a deterministic CLI solver for one-variable linear and quadratic equations. Parse both sides, normalize them to ax^2 + bx + c = 0, combine like terms, classify the equation, and emit a step-by-step transcript in which every transformation preserves the solution set. Handle unique linear solutions, two real roots, a repeated root, no real roots, identities with infinitely many solutions, and contradictions with no solutions. Verify every numeric root by substituting the unrounded value into the original equation and reporting the residual.
Real World Outcome
Given 3x - 7 = 2x + 5, the program explains subtracting the right side, combining terms, and obtaining x = 12, then prints a zero residual. Given x^2 - 5x + 6 = 0, it reports discriminant 1, sorted roots 2 and 3, and two substitution checks. Unsupported expressions, division-by-zero transformations, and malformed input return stable error classes and exit codes.
Core Question
Why does applying the same legal operation to both sides preserve an equation’s truth, and how can software make that invariant auditable?
Concepts You Must Understand First
- Equality-preserving transformations: adding, subtracting, multiplying, or dividing both sides, with nonzero constraints.
- Terms, coefficients, degree, and canonical form: normalize diverse syntax into a small coefficient representation.
- Linear equation classification:
bx + c = 0, includingb = 0identity and contradiction cases. - Quadratic formula and discriminant: root count follows from
D = b^2 - 4ac. See any high-school algebra text, quadratic equations chapter. - Residual validation: computation and display rounding must remain separate.
Build Milestones
- Parse signed constant,
x, andx^2terms on both sides, including implicit coefficients such as-x. - Normalize to coefficients
(a, b, c)and log explicit equality-preserving steps. - Implement complete linear classification, then quadratic discriminant branching and sorted roots.
- Substitute solutions into the original parsed expressions and enforce a documented tolerance.
- Add transcript/golden tests for every solution class and invalid-input path.
Hints in Layers
- Represent a polynomial as a map from degree to coefficient; moving a term means subtracting its coefficient.
- Dispatch after normalization: quadratic when
a != 0, linear whena == 0andb != 0, otherwise classifyc. - Generate explanations from the same transformation objects that modify coefficients so text cannot drift from computation.
Common Pitfalls and Debugging
- Symptom:
0x = 0causes division by zero. Cause: the solver assumed every normalized equation has a unique solution. Fix: classify zero coefficients before dividing. - Symptom: the printed steps yield a different equation from the solver. Cause: explanation strings were handwritten separately. Fix: store operation, operand, before, and after states together.
- Symptom: a displayed rounded root fails substitution. Cause: the rounded display value was reused internally. Fix: retain full precision for validation.
Definition of Done
- Linear and quadratic inputs normalize to a documented canonical form.
- All real solution-set classifications are implemented explicitly.
- Every algebra step preserves equality and can be replayed from the transcript.
- Every reported root passes substitution within tolerance.
- Tests include malformed syntax, reducible quadratics, identities, and contradictions.
Navigation
Previous: Project 2 · Complete learning path · Next: Project 4