A complete polynomial equation solver written in Python.
Handles equations up to degree 2, including real and complex solutions.
Fully compliant with the 42 subject — including mandatory and bonus parts.
ComputorV1 parses, reduces, and solves polynomial equations of the form:
a * X^0 + b * X^1 + c * X^2 = d * X^0 + ...
It supports:
- Reduction to canonical form
- Degree detection
- Degree 0 resolution
- Degree 1 resolution
- Degree 2 resolution
- Real solutions
- Complex solutions
- Fraction input (bonus)
- Natural input format (bonus)
- Irreducible fraction output (bonus)
- Error handling
- Intermediate step display (bonus)
ComputorV1/
│
├── mandatory/
│ ├── main.py
│ ├── parser.py
│ ├── reducer.py
│ ├── solver.py
│ └── math_utils.py
│
├── bonus/
│ ├── main.py
│ ├── parser.py
│ ├── reducer.py
│ ├── solver.py
│ └── math_utils.py
│
└── README.md
- Accept one equation as argument
- Display reduced form
- Display polynomial degree
- Solve degree 0, 1, 2
- Refuse degree > 2
- Handle negative numbers
- Handle decimals
- Handle fractions (if implemented in mandatory)
- Never crash
- Never infinite loop
cd mandatory
python3 main.py "5 * X^0 + 4 * X^1 - 9.3 * X^2 = 1 * X^0"python3 main.py "42 * X^0 = 42 * X^0"Expected:
Any real number is a solution.
python3 main.py "4 * X^0 = 8 * X^0"Expected:
No solution.
python3 main.py "2 * X^1 = 4 * X^0"Expected:
2
python3 main.py "1 * X^2 - 5 * X^1 + 6 * X^0 = 0"Roots:
2
3
python3 main.py "1 * X^2 + 1 * X^0 = 0"Expected:
-0 + 1i
-0 - 1i
python3 main.py "1 * X^3 + 1 * X^0 = 0"Expected:
The polynomial degree is strictly greater than 2, I can't solve.
The bonus extends the solver significantly.
Accepts:
X = 0
4X = 8
2X + 1 = 0
5 + X^2 = X^2
5/2 * X^0 = 1
Interpreted as:
2.5 * X^0 = 1
2X = 1
Outputs:
1/2
5 * X^0 + 3 * X^1 + 3 * X^2 = 1 * X^0
Output:
-1/2 + 1.040833i
-1/2 - 1.040833i
Rational parts simplified. Irrational parts kept as clean decimals.
Detects:
- Missing '='
- Invalid characters
- Invalid variable names
- Invalid power syntax
- Division by zero in fractions
- Malformed equations
Never prints Python traceback.
Displays:
- Parsing phase
- Reduction phase
- Discriminant value
- Solution type decision
A polynomial is an expression of the form:
[ P(X) = a_0 + a_1X + a_2X^2 + ... ]
The degree is the highest exponent.
[ Delta = b^2 - 4ac ]
- Δ > 0 → Two real roots
- Δ = 0 → One real root
- Δ < 0 → Two complex roots
When Δ < 0:
[ X = \frac{-b \pm i\sqrt{-\Delta}}{2a} ]
Used to simplify fractions:
gcd(a, b)
Custom square root implementation:
sqrt_newton(n)
Avoids forbidden math functions.
The program guarantees:
- No infinite loops
- No division by zero
- No float explosion
- Safe epsilon comparisons
- Clean error reporting
- Controlled fraction denominator growth
ComputorV1 is not just a formula implementation.
It demonstrates:
- Algebra mastery
- Numerical reasoning
- Robust parsing
- Defensive programming
- Modular architecture
- Clean CLI design
- Mathematical rigor
42 Network — ComputorV1 Project