Henry wrote:
> Does anyone have J code for multiplying two numbers expressed as 
> simple continued fractions, producing a continued-fraction result?

Roger responded:
>  As a first approximation, convert to two single (rational) numbers; 
>  mutliply; convert back.  *&.conv

Here's one way to do that:

           NB.  Continued fraction to decimal
           cf2d =:  (+%)/                              
           
           NB.  Decimal to continued fraction
           d2cf =:  }:@:<.@:(%@:(-<.)^:(_&>)^:a:&.x:)  
           
           NB.  continued fraction <-> decimal   
           cf   =:  cf2d :. d2cf
           
           NB.  Multiply continued fractions
           cfM  =:  *&.cf

I'm sure there are superior algorithms, in particular for  d2cf  .  I'm
thinking along the line of Euler's GCD algorithm.  There's a lot of depth
hidden in J's rational numbers and the relevant primitives (e.g.  +.  | 
#:  ).

There might also be a direct way (without the intermediate conversion to
decimal), but no ideas occur to me, personally.

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

Reply via email to