continued fraction
Mathworld





a

0


+









b

1












a

1


+









b

2












a

2


+









b

3












a

3


+
⋱

















{\displaystyle a_{0}+{\cfrac {b_{1}}{a_{1}+{\cfrac {b_{2}}{a_{2}+{\cfrac {b_{3}}{a_{3}+\ddots }}}}}}}


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 




a

0


=
1


{\displaystyle a_{0}=1}

 then 




a

N


=
2


{\displaystyle a_{N}=2}

. 




b

N




{\displaystyle b_{N}}

 is always 



1


{\displaystyle 1}

.







2


=
1
+








1









2
+








1









2
+








1









2
+
⋱

















{\displaystyle {\sqrt {2}}=1+{\cfrac {1}{2+{\cfrac {1}{2+{\cfrac {1}{2+\ddots }}}}}}}


For Napier's Constant, use 




a

0


=
2


{\displaystyle a_{0}=2}

, then 




a

N


=
N


{\displaystyle a_{N}=N}

. 




b

1


=
1


{\displaystyle b_{1}=1}

 then 




b

N


=
N
−
1


{\displaystyle b_{N}=N-1}

.





e
=
2
+








1









1
+








1









2
+








2









3
+








3









4
+
⋱






















{\displaystyle e=2+{\cfrac {1}{1+{\cfrac {1}{2+{\cfrac {2}{3+{\cfrac {3}{4+\ddots }}}}}}}}}


For Pi, use 




a

0


=
3


{\displaystyle a_{0}=3}

 then 




a

N


=
6


{\displaystyle a_{N}=6}

. 




b

N


=
(
2
N
−
1

)

2




{\displaystyle b_{N}=(2N-1)^{2}}

.





π
=
3
+








1









6
+








9









6
+








25









6
+
⋱

















{\displaystyle \pi =3+{\cfrac {1}{6+{\cfrac {9}{6+{\cfrac {25}{6+\ddots }}}}}}}




See also

   Continued fraction/Arithmetic for tasks that do arithmetic over continued fractions.


INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE PTR="CARD"
DEFINE JSR="$20"
DEFINE RTS="$60"
 
PROC CoeffA=*(INT n REAL POINTER res)
  [JSR $00 $00 ;JSR to address set by SetCoeffA
   RTS]
 
PROC CoeffB=*(INT n REAL POINTER res)
  [JSR $00 $00 ;JSR to address set by SetCoeffB
   RTS]
 
PROC SetCoeffA(PTR p)
  PTR addr
 
  addr=CoeffA+1 ;location of address of JSR
  PokeC(addr,p)
RETURN
 
PROC SetCoeffB(PTR p)
  PTR addr
 
  addr=CoeffB+1 ;location of address of JSR
  PokeC(addr,p)
RETURN
 
PROC Calc(PTR funA,funB INT count REAL POINTER res)
  INT i
  REAL a,b,tmp
 
  SetCoeffA(funA)
  SetCoeffB(funB)
 
  IntToReal(0,res)
  i=count
  WHILE i>0
  DO
    CoeffA(i,a)
    CoeffB(i,b)
    RealAdd(a,res,tmp)
    RealDiv(b,tmp,res)
    i==-1
  OD
  CoeffA(0,a)
  RealAdd(a,res,tmp)
  RealAssign(tmp,res)
RETURN
 
PROC sqrtA(INT n REAL POINTER res)
  IF n>0 THEN
    IntToReal(2,res)
  ELSE
    IntToReal(1,res)
  FI
RETURN
 
PROC sqrtB(INT n REAL POINTER res)
  IntToReal(1,res)
RETURN
 
PROC napierA(INT n REAL POINTER res)
  IF n>0 THEN
    IntToReal(n,res)
  ELSE
    IntToReal(2,res)
  FI
RETURN
 
PROC napierB(INT n REAL POINTER res)
  IF n>1 THEN
    IntToReal(n-1,res)
  ELSE
    IntToReal(1,res)
  FI
RETURN
 
PROC piA(INT n REAL POINTER res)
  IF n>0 THEN
    IntToReal(6,res)
  ELSE
    IntToReal(3,res)
  FI
RETURN
 
PROC piB(INT n REAL POINTER res)
  REAL tmp
 
  IntToReal(2*n-1,tmp)
  RealMult(tmp,tmp,res)
RETURN
 
PROC Main()
  REAL res
 
  Put(125) PutE() ;clear the screen
 
  Calc(sqrtA,sqrtB,50,res)
  Print(" Sqrt2=") PrintRE(res)
 
  Calc(napierA,napierB,50,res)
  Print("Napier=") PrintRE(res)
 
  Calc(piA,piB,500,res)
  Print("    Pi=") PrintRE(res)
RETURN