def p(s):print(s)
def o1():
p("=POLY EVAL=")
n=int(input("deg:"))
c=[]
for i in range(n+1):
c.append(float(input("c:")))
x=float(input("x:"))
r=0.0
for k in c:r=r*x+k
p("p(x)="+str(r))
def menu():
while True:
p("=POLYEVAL=")
p("1:EVAL")
p("0:EXIT")
c=input("?>")
if c=="1":o1()
elif c=="0":break
menu()Tier 1 · precalculus
Poly Eval
Evaluate polynomial via Horner's method.
Files (1)
How to use it
Evaluates a polynomial at a given x using Horner's method (fast, no **).
Launch POLYEVAL from prgm, choose 1:EVAL, then:
deg:— degree of the polynomial (e.g. 2 for a quadratic).c:— the leading coefficient, then each lower-degree coefficient in order, highest first, including zeros.x:— the value to evaluate at.
Output is p(x)=. Enter exactly deg+1 coefficients.
Example problem
Evaluate p(x) = 2x + 5 at x = 3.
Degree 1, coefficients [2, 5] (highest-first).
- Run
POLYEVAL, choose1:EVAL. deg:→ 1.c:→ 2,c:→ 5.x:→ 3.- Output:
p(x)=11.0.
Verify: 2(3) + 5 = 11. ✓
TI-84 Plus CE — try before committingoff
TI-84 Plus CE
(calculator off) Press ON to start.
Test cases (2)
| ID | Inputs | Expected contains |
|---|---|---|
| cubic-x2 | 1, 3, 1, 2, 3, 4, 2, 0 | p(x)= |
| linear-x3 | 1, 1, 2, 5, 3, 0 | p(x)=11.0 |