Limit of a polynomial quotient

When p(a) = 0, the quotient p(x)/(x - a) has the form 0/0 at x = a, but it can still have a limit.

What to build

Estimate the limit of p(x)/(x − a) as x approaches a, by averaging the values just right and just left of a.

Requirements

  • Evaluate the polynomial p with Horner's rule or a loop.
  • Use h = 0.001 and compute the right value p(a + h)/h and the left value p(a − h)/(−h).
  • Print the average of the two, with 4 decimal places.

Input

Line 1: the whole-number coefficients of p, highest power first (2 to 7 numbers). Line 2: a whole number a with p(a) = 0.

Output

One line: limit L, with L to 4 decimal places.

Constraints

The coefficients and a are between -20 and 20.

Example

Input:

1 0 -1
1

Output:

limit 2.0000

Why it works

Because p(a) = 0, p(x) contains the factor (x − a), so p(x)/(x − a) is an ordinary polynomial near a and its limit is its value at a. Averaging both sides cancels most of the error.

Examples

Example 1 · Input
1 0 -1
1
Expected output
limit 2.0000
Example 2 · Input
1 -3 2
2
Expected output
limit 1.0000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

You cannot put x = a into p(x)/(x - a): it gives 0/0. Use x = a + h and x = a - h with a small h.

Hint 2

With h = 0.001, p(a + h)/h is the right-hand value, and p(a - h)/(-h) is the left-hand value.

Hint 3

Average the two sides. Print with `:.4f`.

Solution

Show one correct solution (try it yourself first)
coeffs = [int(c) for c in input().split()]
a = int(input())

def p(x):
    total = 0.0
    for c in coeffs:
        total = total * x + c
    return total

h = 0.001
right = p(a + h) / h
left = p(a - h) / (-h)
print(f"limit {(right + left) / 2:.4f}")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Functions and limits →