Continued fraction/Arithmetic/Construct from rational number

From Rosetta Code
Revision as of 12:43, 4 February 2013 by Nigel Galloway (talk | contribs) (Created page with "{{task}}To understand this task in context please see http://rosettacode.org/wiki/Continued_fraction_arithmetic Continued fraction arithmetic The purpose of this task is ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Continued fraction/Arithmetic/Construct from rational number
You are encouraged to solve this task according to the task description, using any language you may know.

To understand this task in context please see [Continued fraction arithmetic]

The purpose of this task is to write a function r2cf(int N1, int N2), or r2cf(Fraction N), which will output a continued fraction assuming:

N1 is the numerator
N2 is the denominator

The function should output its results one digit at a time each time it is called, in a manner sometimes described as lazy evaluation.

To achieve this it must determine: the integer part; and remainder part, of N1 divided by N2. It then sets N1 to N2 and N2 to the determined remainder part. It then outputs the determined integer part. It does this until N2 is zero.

Demonstrate the function by outputing the continued fraction for:

1/2
3
23/8
13/11
22/7

sqrt2 should approach cf 1 2 2 2 2 ... try ever closer rational approximations until bordom gets the better of you:

14142,10000
141421,100000
1414214,1000000
4142136,10000000

Demonstrate that this function may be used as generator a in [Continued fraction] and obtain a floating point value for 23/8, 13/11, and 22/7