A geometric series

A geometric series starts at a and multiplies by r each time: a + ar + ar² + …. Print the sum of its first n terms, and its infinite sum if it converges.

Input

One line: a and r (decimals, a is not 0) and n (a whole number, 1 to 1000).

Output

Two lines: partial S with the sum of the first n terms, then infinite T if r is strictly between −1 and 1, or infinite diverges otherwise. Numbers to 6 decimal places.

Examples

Example 1 · Input
1 0.5 10
Expected output
partial 1.998047
infinite 2.000000
Example 2 · Input
3 -0.5 5
Expected output
partial 2.062500
infinite 2.000000

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

Hints

Hint 1

Keep a running term: start at a and multiply by r after adding it.

Hint 2

The infinite sum a ÷ (1 − r) only exists when r is strictly between −1 and 1.

Solution

Show one correct solution (try it yourself first)
a, r, n = input().split()
a, r, n = float(a), float(r), int(n)
total, term = 0.0, a
for _ in range(n):
    total += term
    term *= r
print(f"partial {total:.6f}")
print(f"infinite {a / (1 - r):.6f}" if -1 < r < 1 else "infinite diverges")

Lesson: Sequences and series →