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.


       identification division.
       program-id. show-continued-fractions.
 
       environment division.
       configuration section.
       repository.
           function continued-fractions
           function all intrinsic.
 
       procedure division.
       fractions-main.
 
       display "Square root 2 approximately   : "
               continued-fractions("sqrt-2-alpha", "sqrt-2-beta", 100)
       display "Napier constant approximately : "
               continued-fractions("napier-alpha", "napier-beta", 40)
       display "Pi approximately              : "
               continued-fractions("pi-alpha", "pi-beta", 10000)
 
       goback.
       end program show-continued-fractions.
 
      *> **************************************************************
       identification division.
       function-id. continued-fractions.
 
       data division.
       working-storage section.
       01 alpha-function       usage program-pointer.
       01 beta-function        usage program-pointer.
       01 alpha                usage float-long.
       01 beta                 usage float-long.
       01 running              usage float-long.
       01 i                    usage binary-long.
 
       linkage section.
       01 alpha-name           pic x any length.
       01 beta-name            pic x any length.
       01 iterations           pic 9 any length.
       01 approximation        usage float-long.
 
       procedure division using
           alpha-name beta-name iterations
           returning approximation.
 
       set alpha-function to entry alpha-name
       if alpha-function = null then
           display "error: no " alpha-name " function" upon syserr
           goback
       end-if
       set beta-function to entry beta-name
       if beta-function = null then
           display "error: no " beta-name " function" upon syserr
           goback
       end-if
 
       move 0 to alpha beta running
       perform varying i from iterations by -1 until i = 0
           call alpha-function using i returning alpha
           call beta-function using i returning beta
           compute running = beta / (alpha + running)
       end-perform
       call alpha-function using 0 returning alpha
       compute approximation = alpha + running
 
       goback.
       end function continued-fractions.
 
      *> ******************************
       identification division.
       program-id. sqrt-2-alpha.
 
       data division.
       working-storage section.
       01 result               usage float-long.
 
       linkage section.
       01 iteration            usage binary-long unsigned.
 
       procedure division using iteration returning result.
       if iteration equal 0 then
           move 1.0 to result
       else
           move 2.0 to result
       end-if
 
       goback.
       end program sqrt-2-alpha.
 
      *> ******************************
       identification division.
       program-id. sqrt-2-beta.
 
       data division.
       working-storage section.
       01 result               usage float-long.
 
       linkage section.
       01 iteration            usage binary-long unsigned.
 
       procedure division using iteration returning result.
       move 1.0 to result
 
       goback.
       end program sqrt-2-beta.
 
      *> ******************************
       identification division.
       program-id. napier-alpha.
 
       data division.
       working-storage section.
       01 result               usage float-long.
 
       linkage section.
       01 iteration            usage binary-long unsigned.
 
       procedure division using iteration returning result.
       if iteration equal 0 then
           move 2.0 to result
       else
           move iteration to result
       end-if
 
       goback.
       end program napier-alpha.
 
      *> ******************************
       identification division.
       program-id. napier-beta.
 
       data division.
       working-storage section.
       01 result               usage float-long.
 
       linkage section.
       01 iteration            usage binary-long unsigned.
 
       procedure division using iteration returning result.
       if iteration = 1 then
           move 1.0 to result
       else
           compute result = iteration - 1.0
       end-if
 
       goback.
       end program napier-beta.
 
      *> ******************************
       identification division.
       program-id. pi-alpha.
 
       data division.
       working-storage section.
       01 result               usage float-long.
 
       linkage section.
       01 iteration            usage binary-long unsigned.
 
       procedure division using iteration returning result.
       if iteration equal 0 then
           move 3.0 to result
       else
           move 6.0 to result
       end-if
 
       goback.
       end program pi-alpha.
 
      *> ******************************
       identification division.
       program-id. pi-beta.
 
       data division.
       working-storage section.
       01 result               usage float-long.
 
       linkage section.
       01 iteration            usage binary-long unsigned.
 
       procedure division using iteration returning result.
       compute result = (2 * iteration - 1) ** 2
 
       goback.
       end program pi-beta.
 