Continued fraction: Difference between revisions

m
<math> is awesome
m (Shrink the image a bit, fix a typo)
m (<math> is awesome)
Line 1:
{{draft task}}A number may be represented as a continued fraction (see http://mathworld.wolfram.com/ContinuedFraction.html) as follows:
 
:<math>cf = a_1 + \cfrac{b_1}{a_2 + \cfrac{b_2}{a_3 + \cfrac{b_3}{\mathrm{etc\ldots}}}}</math>
[[Image:Continued_Fraction.PNG|center|Continued Fraction|200px]]
 
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.
 
For the square root of 2, bN<math>b_N</math> is always <math>1</math>. a1<math>a_1</math> is <math>1</math> then aN<math>a_N</math> is <math>2</math>.
 
For Napier's Constant, a1<math>a_1</math> is <math>2</math>, then aN<math>a_N</math> is <math>N-1</math>. b1<math>b_1</math> is <math>1</math> then bN<math>b_N</math> is <math>N-1</math>.
 
For Pi, a1<math>a_1</math> is 3 then aN<math>a_N</math> is always <math>6</math>. bN<math>b_N</math> is <math>(2N-1)(2N-1)</math>.
 
=={{header|Python}}==
<lang python># The continued Fraction
 
<lang python>
 
# The continued Fraction
def CF(a, b, t):
if 0 < t:
Line 86 ⟶ 82:
cf = CF(Pi_a(), Pi_b(), 950)
print(pRes(cf, 10))
#3.1415926532</lang>
 
</lang>
Anonymous user