□ Numerical methods · iteration to a tolerance
Bisection root finder
Solve f(x) = 0 for a polynomial by repeatedly halving an interval that contains the root.
- Level 4 · Systems Engineer
- 6 tests
- 🐞 debug variant
- technically checked
Your task
Many design equations cannot be rearranged for x, so engineers solve them numerically. If f(lo) and f(hi) have opposite signs, a root lies between them; halve the interval, keep the half where the sign still changes, and repeat.
Write:
- poly(coeffs, x): evaluate a polynomial whose coefficients are listed highest power first, e.g. [1, 0, −2, −5] is x³ − 2x − 5.
- bisect(coeffs, lo, hi, tol): return an x within
tolof a root. If f(lo) and f(hi) have the same sign, return null (no bracket). Stop when (hi − lo) / 2 < tol and return the midpoint. Never loop more than 200 times.
Hints (0 of 3 shown)
What gets checked
- poly([1, 0, -2, -5], 2) → -1
- poly([2, 3], 4) → 11
- bisect([1, 0, -2, -5], 2, 3, 1e-9) → ≈ 2.094551481 (± 0.000001)
Plus 3 hidden tests for edge cases. They run in the sandbox like the others; only the result is shown.
Tab indents · Esc then Tab leaves the editor · Ctrl/⌘ Enter runs · Ctrl/⌘ Shift Enter checks · Ctrl/⌘ / comments
Press Run to see what your program prints.
Your code runs only in this browser, inside an isolated sandbox with no network access, and stops after 3 s. Nothing you type is sent to Tactiverge; drafts and progress are saved in this browser only.