#B2148. Recursive Continued Fraction Calculation

    ID: 11230 Type: Default 1000ms 256MiB

Recursive Continued Fraction Calculation

Recursive Continued Fraction Calculation

Given a mathematical function defined by the continued fraction:

$$ f(x,n)= \frac{x}{n+\frac{x}{(n-1)+\frac{x}{(n-2)+\cdots+\frac{x}{1+x}}}} $$

Implement a recursive function to compute f(x, n) for given values of x and a positive integer n. The recursion should stop when the denominator reaches the term 1+x (i.e. when n reaches 0, return x). In other words, define the function recursively as:

$$\text{if } n=0,\ f(x,0)=x, \quad \text{else } f(x,n)= \frac{x}{n+f(x,n-1)} $$

Print the result rounded to six decimal places.

inputFormat

The input consists of a single line containing two numbers separated by spaces:

  • x: a floating-point number.
  • n: a positive integer.

outputFormat

Output the computed value of f(x, n) rounded to six decimal places.

sample

5 1
0.833333