You might find these verbs of some use.  I cobbled them
together from some source which I should acknowledge,
but forget right now and it's late!

NB. Somewhat more exact than working on %:y as the input.
NB. convergents of continued fraction for %: y
rtn =: 3 : 0
y rtn y
:
'h t' =: ({.;}.) myroot y   NB. continued fraction for %:y
y  =. h, t$~ x.-1
ab =. ,r =. 1 2$1,~{. y
a0b0 =. 1 0
for_yi. }. y do.
   nab =. x: a0b0 + yi * ab
   r =. r,ab =. nab [ a0b0 =. ab
end.
r
)

NB. continued fraction elements for %: y
myroot =: 3 : 0
if. (=<.&.%:) y do. %:y return. end.
a =. e =. a0=. <.k=. %:k2=. y
k21 =. k2, n=.b=.1
end =. 0
while. end = 0 do.
   d      =. -/k21 * *:b,e
   'd b e'=. (% | @:+./) d,'b e'=. n * b,e NB. reduce
   a      =. a, a0 =. <. d %~ e + b * k
   end    =. 1 = n =. d
   e      =. e -~ d * a0
end.
a
)

    myroot 7
2 1 1 1 4
    rtn 7
  2  1
  3  1
  5  2
  8  3
37 14
45 17
82 31

Mike
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to