Left-edge rectangle sum

The simplest way to estimate an area is to add up rectangles that touch the curve on their left side.

What to build

Estimate the area under a polynomial with the left-edge rule.

Requirements

  • Read the coefficients of the polynomial, highest power first.
  • Cut [a, b] into n equal strips and use the height at each strip's left edge.
  • Print the sum to 6 decimal places.

Input

Line 1: 1 to 6 whole-number coefficients, highest power first. Line 2: a, b and n, where a and b are decimals with a less than b, and n is a whole number from 1 to 1000.

Output

One line: left S, with S to 6 decimal places.

Constraints

The coefficients and the limits are between -10 and 10.

Example

Input:

1 0 0
0 3 4

Output:

left 5.906250

Why it works

Each rectangle has width (b − a) ÷ n and height equal to the curve at its left edge. For a rising curve this always falls short of the true area, and the gap shrinks as n grows.

Examples

Example 1 · Input
1 0 0
0 3 4
Expected output
left 5.906250
Example 2 · Input
2
1 5 10
Expected output
left 8.000000

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

Hints

Hint 1

The strip width is (b - a) / n.

Hint 2

The i-th left edge is a + i * width, for i from 0 to n - 1.

Hint 3

Add f(edge) * width for each strip and print with `:.6f`.

Solution

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

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

width = (b - a) / n
total = 0.0
for i in range(n):
    total += f(a + i * width) * width
print(f"left {total + 0.0:.6f}")

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

Lesson: Integrals: area under a curve →