/**********************************************
For integer a, we have the following continued
fraction:
a + 1/(a + 1/(a + 1/(a + ...
Of course this is an infinite continued fraction,
so we could also define f(a,n) to be the first n
terms, so that:
f(a,0) = a
f(a,1) = a + 1/a
f(a,2) = a + 1/(a + 1/a)
...
Implement f(a,n) recursively.
**********************************************/
#include <iostream>
#include <iomanip>
using namespace std;
double f(int a, int n);
int main()
{
// Get integer n, n >= 0
int n,a;
cout << "Enter non-negative integer: ";
cin >> n;
cout << "Enter value a: ";
cin >> a;
cout << "f(" << a << ',' << n << ") = ";
cout << setprecision(20) << f(a,n) << endl;
return 0;
}
double f(int a, int n)
{
if (n == 0)
return a;
return a + 1/f(a,n-1);
}