Skip to content
Onsite

GP and Recursion

EasyP3Coding

Given integers n and r, compute

S = 1 + 1/r + 1/r^2 + ... + 1/r^n

using recursion and return S as a number. Answers within 1e-5 of the exact value are accepted (the original problem prints four decimals).

Example 1

Input
n = 3, r = 2
Output
1.875
Why
1 + 0.5 + 0.25 + 0.125 = 1.875.

Example 2

Input
n = 0, r = 5
Output
1

Example 3

Input
n = 1, r = 1
Output
2

Constraints

  • 0 <= n <= 100
  • 1 <= r <= 100

Hints

Run checks the 3 visible tests, Submit all 9.