⍝ Continued Fractions:

    ⎕DCT DCT ← ⎕CT ⎕DCT                 ⍝ for decimal floating point

    cfract 5÷8                          ⍝ rational numbers have a finite CF.
0 1 1 1 2

    cfract 67÷29
2 3 4 2

    cfract¨ ¯4 to 4                     ⍝ whole numbers, including 0.
┌──┬──┬──┬──┬─┬─┬─┬─┬─┐
│¯4│¯3│¯2│¯1│0│1│2│3│4│
└──┴──┴──┴──┴─┴─┴─┴─┴─┘

    cfract *1                           ⍝ e has an infinite but regular CF.
2 1 2 1 1 4 1 1 6 1 1 8 1 1 10 1 1 12

    +∘÷/ cfract *1                      ⍝ reduction reconstitutes e.
2.718281828

    cfract ○1                           ⍝ irregular infinite CF for pi.
3 7 15 1 292 1 1 1 2 1 4

    cfract root 2                       ⍝ CF for sqrt(2).
1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

    cfract 0.5×1+root 5                 ⍝ CF for golden ratio.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2

    cfract ¯2.3                         ⍝ negative argument.
¯3 1 2 3

    rational +∘÷/¨ 1<⍳¨⍳10              ⍝ Fibonacci sequences.
0 1 1 2 3 5  8 13 21 34
1 1 2 3 5 8 13 21 34 55
                                        ⍝ Some regular CFs:
    cfract 0.5×3+root 13
3 3 3 3 3 3 3 3 3 3 3 3 3 3

    cfract (4+root 37)÷7
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2

    14↑cfract 3○1                       ⍝ tan(1)
1 1 1 3 1 5 1 7 1 9 1 11 1 13

:If ⎕FR=1287                            ⍝ successive rational approximations to Pi.
        rational +∘÷/¨ ,\ 0 cfract ○1
    3 22 333 355 103993 104348 208341 312689 833719 1146408 5419351
    1  7 106 113  33102  33215  66317  99532 265381  364913 1725033
:Else
        rational +∘÷/¨ ,\ 0 cfract ○1
    3 22 333 355 103993 104348 208341 312689 833719 1146408 4272943 5419351 5419351 5419351
    1  7 106 113  33102  33215  66317  99532 265381  364913 1360120 1725033 1725033 1725033
:EndIf

    seq ← {+∘÷/¨,\cfract ⍵}                 ⍝ successive reductions.
    err ← {10⍟|(⍵-⍺⍺ ⍵)÷⍵}                  ⍝ base 10 log error factor
    col ← {6 2⍕⍪⍵}                          ⍝ formatted column.

    vals ← (*1) (○1) (2*÷2) (0.5×1+5*÷2)    ⍝ exp, Pi, sqrt 2, phi.

:If ⎕FR=1287

    ⍕ col∘(seq err)¨ vals       ⍝ 10∘⍟ errors in successive approximations.
  ¯0.58   ¯1.35   ¯0.53   ¯0.42 
  ¯0.98   ¯3.40   ¯1.22   ¯0.63 
  ¯1.72   ¯4.58   ¯2.00   ¯1.14 
  ¯1.93   ¯7.07   ¯2.76   ¯1.52 
  ¯2.83   ¯9.74   ¯3.53   ¯1.95 
  ¯3.76   ¯9.98   ¯4.29   ¯2.37 
  ¯3.91  ¯10.41   ¯5.06   ¯2.79 
  ¯4.99  ¯11.03   ¯5.82   ¯3.20 
  ¯6.08  ¯11.56   ¯6.59   ¯3.62 
  ¯6.19  ¯12.29   ¯7.35   ¯4.04 
  ¯7.39  ¯14.15   ¯8.12   ¯4.46 
  ¯8.61           ¯8.89   ¯4.88 
  ¯8.69           ¯9.65   ¯5.29 
  ¯9.99          ¯10.42   ¯5.71 
 ¯11.30          ¯11.18   ¯6.13 
 ¯11.37          ¯11.95   ¯6.55 
 ¯12.75          ¯12.71   ¯6.97 
 ¯14.14          ¯13.48   ¯7.38 
                 ¯14.24   ¯7.80 
                          ¯8.22 
                          ¯8.64 
                          ¯9.05 
                          ¯9.47 
                          ¯9.89 
                         ¯10.31 
                         ¯10.73 
                         ¯11.14 
                         ¯11.56 
                         ¯11.98 
                         ¯12.40 
                         ¯12.82 
                         ¯13.23 
                         ¯14.07 
:Else
    ⍕ col∘(seq err)¨ vals       ⍝ 10∘⍟ errors in successive approximations.
  ¯0.58   ¯1.35   ¯0.53   ¯0.42 
  ¯0.98   ¯3.40   ¯1.22   ¯0.63 
  ¯1.72   ¯4.58   ¯2.00   ¯1.14 
  ¯1.93   ¯7.07   ¯2.76   ¯1.52 
  ¯2.83   ¯9.74   ¯3.53   ¯1.95 
  ¯3.76   ¯9.98   ¯4.29   ¯2.37 
  ¯3.91  ¯10.41   ¯5.06   ¯2.79 
  ¯4.99  ¯11.03   ¯5.82   ¯3.20 
  ¯6.08  ¯11.56   ¯6.59   ¯3.62 
  ¯6.19  ¯12.29   ¯7.35   ¯4.04 
  ¯7.39  ¯14.15   ¯8.12   ¯4.46 
  ¯8.61           ¯8.89   ¯4.88 
  ¯8.69           ¯9.65   ¯5.29 
  ¯9.99          ¯10.42   ¯5.71 
 ¯11.30          ¯11.18   ¯6.13 
 ¯11.37          ¯11.95   ¯6.55 
 ¯12.75          ¯12.71   ¯6.97 
 ¯14.13          ¯13.48   ¯7.38 
                 ¯14.24   ¯7.80 
                          ¯8.22 
                          ¯8.64 
                          ¯9.05 
                          ¯9.47 
                          ¯9.89 
                         ¯10.31 
                         ¯10.73 
                         ¯11.14 
                         ¯11.56 
                         ¯11.98 
                         ¯12.40 
                         ¯12.82 
                         ¯13.24 
                         ¯14.07 
:EndIf

    ×⍨∘{+∘÷/1 40/⍵}¨(⍳10)×⊂1 2          ⍝ (cf ⍵×1 2 2 ...)*2 ←→ 1+⍵*2.
2 5 10 17 26 37 50 65 82 101

    ×⍨∘{+∘÷/1 40/⍵}¨(⍳10)×⊂3 6          ⍝ (cf ⍵×3 6 6 ...)*2 ←→ 1+(3×⍵)*2.
10 37 82 145 226 325 442 577 730 901

    ⎕DCT ← DCT                          ⍝ restore DCT

⍝∇ cfract rational root to

Back to: code

Back to: Workspaces