Fraction by recursion(2)

Feb 22, 2017 at 8:32pm
Hello, it is quite easy for me to divide 1/(2/(3/4)) by recursion (if k = 3 then output will be 0.375), but the trouble is that i dont know how to add numbers to this like 1/(1 + [2/(2 + [3/4])])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "stdafx.h"
#include <iostream>
using namespace std;

double Division(double start, double k);

int main()
{
	double k, start;
	start = 1;
	cout << "Enter k: ";
	cin >> k;

	if (k <= 2)
	{
		cout << "k should be higher than 2 !" << endl;
	}
	else if (k >= 3)
	{
		cout << "The result is " << Division(start, k + 1) << endl;
	}
	return 0;
}

}

double Division(double start, double k)
{
	if (start <= k)
	{
		return start / Division(start + 1, k);
	}
	else
	{
		return 1;
	}
}
Last edited on Feb 22, 2017 at 8:45pm
Feb 22, 2017 at 9:10pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

//============================

double continuedFraction( int k, int n )
{
   if ( n == k ) return 1.0;
   else          return n / ( n + continuedFraction( k, n + 1 ) );
}
//============================

int main()
{
   int k;
   cout << "Input last term, k: ";
   cin >> k;

   cout << "Continued fraction partial sum = " << continuedFraction( k, 1 );
}


Input last term, k: 4
Continued fraction partial sum = 0.578947


http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/cfINTRO.html
https://en.wikipedia.org/wiki/Continued_fraction
Last edited on Feb 22, 2017 at 9:19pm
Topic archived. No new replies allowed.