Continued fraction: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Shrink the image a bit, fix a typo)
m (<math> is awesome)
Line 1: Line 1:
{{draft task}}A number may be represented as a continued fraction (see http://mathworld.wolfram.com/ContinuedFraction.html) as follows:
{{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 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.
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 is always 1. a1 is 1 then aN is 2.
For the square root of 2, <math>b_N</math> is always <math>1</math>. <math>a_1</math> is <math>1</math> then <math>a_N</math> is <math>2</math>.


For Napier's Constant a1 is 2, then aN is N-1. b1 is 1 then bN is N-1.
For Napier's Constant, <math>a_1</math> is <math>2</math>, then <math>a_N</math> is <math>N-1</math>. <math>b_1</math> is <math>1</math> then <math>b_N</math> is <math>N-1</math>.


For Pi a1 is 3 then aN is always 6. bN is (2N-1)(2N-1).
For Pi, <math>a_1</math> is 3 then <math>a_N</math> is always <math>6</math>. <math>b_N</math> is <math>(2N-1)(2N-1)</math>.


=={{header|Python}}==
=={{header|Python}}==
<lang python># The continued Fraction

<lang python>

# The continued Fraction
def CF(a, b, t):
def CF(a, b, t):
if 0 < t:
if 0 < t:
Line 86: Line 82:
cf = CF(Pi_a(), Pi_b(), 950)
cf = CF(Pi_a(), Pi_b(), 950)
print(pRes(cf, 10))
print(pRes(cf, 10))
#3.1415926532
#3.1415926532</lang>

</lang>

Revision as of 21:11, 27 February 2012

Continued fraction is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

A number may be represented as a continued fraction (see http://mathworld.wolfram.com/ContinuedFraction.html) as follows:

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, is always . is then is .

For Napier's Constant, is , then is . is then is .

For Pi, is 3 then is always . is .

Python

<lang python># The continued Fraction def CF(a, b, t):

 if 0 < t:
   a1 = next(a)
   b1 = next(b)
   z  = CF(a, b, t-1)
   return (a1*z[0] + b1*z[1], z[0])
 return (1,1)
  1. Convert the continued fraction to a string

def pRes(cf, d):

 res = str(cf[0] // cf[1])
 res += "."
 x = cf[0] % cf[1]
 while 0 < d:
   x *= 10
   res += str(x // cf[1])
   x = x % cf[1]
   d -= 1
 return res
  1. Test the Continued Fraction for sqrt2

def sqrt2_a():

 yield 1
 while (True):
   yield 2

def sqrt2_b():

 while (True):
   yield 1

cf = CF(sqrt2_a(), sqrt2_b(), 950) print(pRes(cf, 200))

  1. 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147


  1. Test the Continued Fraction for Napier's Constant

def Napier_a():

 yield 2
 n = 1
 while (True):
   yield n
   n += 1

def Napier_b():

 n=1
 yield n
 while (True):
   yield n
   n += 1

cf = CF(Napier_a(), Napier_b(), 950) print(pRes(cf, 200))

  1. 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901
  1. Test the Continued Fraction for Pi

def Pi_a():

 yield 3
 while (True):
   yield 6

def Pi_b():

 x = 1
 while (True):
   yield x*x
   x += 2

cf = CF(Pi_a(), Pi_b(), 950) print(pRes(cf, 10))

  1. 3.1415926532</lang>