34 lines
1.6 KiB
Text
34 lines
1.6 KiB
Text
To understand this task in context please see [[Continued fraction arithmetic]]
|
|
|
|
The purpose of this task is to write a function <math>\mathit{r2cf}(\mathrm{int}</math> <math>N_1, \mathrm{int}</math> <math>N_2)</math>, or <math>\mathit{r2cf}(\mathrm{Fraction}</math> <math>N)</math>, which will output a continued fraction assuming:
|
|
:<math>N_1</math> is the numerator
|
|
:<math>N_2</math> 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 <math>N_1</math> divided by <math>N_2</math>. It then sets <math>N_1</math> to <math>N_2</math> and <math>N_2</math> to the determined remainder part. It then outputs the determined integer part. It does this until <math>\mathrm{abs}(N_2)</math> is zero.
|
|
|
|
Demonstrate the function by outputing the continued fraction for:
|
|
: 1/2
|
|
: 3
|
|
: 23/8
|
|
: 13/11
|
|
: 22/7
|
|
: -151/77
|
|
<math>\sqrt 2</math> should approach <math>[1; 2, 2, 2, 2, \ldots]</math> try ever closer rational approximations until boredom gets the better of you:
|
|
: 14142,10000
|
|
: 141421,100000
|
|
: 1414214,1000000
|
|
: 14142136,10000000
|
|
|
|
Try :
|
|
: 31,10
|
|
: 314,100
|
|
: 3142,1000
|
|
: 31428,10000
|
|
: 314285,100000
|
|
: 3142857,1000000
|
|
: 31428571,10000000
|
|
: 314285714,100000000
|
|
|
|
Observe how this rational number behaves differently to <math>\sqrt 2</math> and convince yourself that, in the same way as <math>3.7</math> may be represented as <math>3.70</math> when an extra decimal place is required, <math>[3;7]</math> may be represented as <math>[3;7,\infty]</math> when an extra term is required.
|