23 lines
1.2 KiB
Text
23 lines
1.2 KiB
Text
A number may be represented as a [[wp:Continued fraction|continued fraction]] (see [http://mathworld.wolfram.com/ContinuedFraction.html Mathworld] for more information) as follows:
|
|
|
|
:<math>a_0 + \cfrac{b_1}{a_1 + \cfrac{b_2}{a_2 + \cfrac{b_3}{a_3 + \ddots}}}</math>
|
|
|
|
The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients:
|
|
|
|
For the square root of 2, use <math>a_0 = 1</math> then <math>a_N = 2</math>. <math>b_N</math> is always <math>1</math>.
|
|
|
|
:<math>\sqrt{2} = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \ddots}}}</math>
|
|
|
|
For Napier's Constant, use <math>a_0 = 2</math>, then <math>a_N = N</math>. <math>b_1 = 1</math> then <math>b_N = N-1</math>.
|
|
|
|
:<math>e = 2 + \cfrac{1}{1 + \cfrac{1}{2 + \cfrac{2}{3 + \cfrac{3}{4 + \ddots}}}}</math>
|
|
|
|
For Pi, use <math>a_0 = 3</math> then <math>a_N = 6</math>. <math>b_N = (2N-1)^2</math>.
|
|
|
|
:<math>\pi = 3 + \cfrac{1}{6 + \cfrac{9}{6 + \cfrac{25}{6 + \ddots}}}</math>
|
|
|
|
|
|
;See also:
|
|
:* [[Continued fraction/Arithmetic]] for tasks that do arithmetic over continued fractions.
|
|
<br><br>
|
|
|