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
Ruby
=begin
Generate a continued fraction from a rational number
Nigel Galloway, February 4th., 2013
=end <lang ruby> def r2cf(n1,n2)
while n2 > 0 t1 = n1/n2; t2 = n2; n2 = n1 - t1 * n2; n1 = t2; yield t1 end
end </lang>
Testing
1/2 <lang ruby>r2cf(1,2) {|n| print "#{n} "}</lang>
- Output:
0 2
3
<lang ruby>r2cf(3,1) {|n| print "#{n} "}</lang>
- Output:
3
23/8 <lang ruby>r2cf(23,8) {|n| print "#{n} "}</lang>
- Output:
2 1 7
13/11 <lang ruby>r2cf(13,11) {|n| print "#{n} "}</lang>
- Output:
1 5 2
22/7 <lang ruby>r2cf(22,7) {|n| print "#{n} "}</lang>
- Output:
3 7
1.4142 <lang ruby>r2cf(14142,10000) {|n| print "#{n} "}</lang>
- Output:
1 2 2 2 2 2 1 1 29
1.4142 <lang ruby>r2cf(141421,100000) {|n| print "#{n} "}</lang>
- Output:
1 2 2 2 2 2 2 3 1 1 3 1 7 2
1.414214 <lang ruby>r2cf(1414214,1000000) {|n| print "#{n} "}</lang>
- Output:
1 2 2 2 2 2 2 2 3 6 1 2 1 12
1.4142136 <lang ruby>r2cf(14142136,10000000) {|n| print "#{n} "}</lang>
- Output:
1 2 2 2 2 2 2 2 2 2 6 1 2 4 1 1 2