LCM also finds a neat use in this problem:

   num_denom=: 1 (*.,%...@+.) ]
   ] y=: (+%)/100$1x
573147844013817084101r354224848179261915075
   num_denom y
573147844013817084101 354224848179261915075
   2 x: y
573147844013817084101 354224848179261915075



----- Original Message -----
From: Roger Hui <[email protected]>
Date: Tuesday, March 3, 2009 21:12
Subject: Re: [Jprogramming] deconstructing rationals
To: Programming forum <[email protected]>

> As others have said, 2 x: y does it.
> The problem is an interesting puzzle
> if you didn't have 2 x: y.  Spoiler below.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>    denominator=: %@(1&+.)
>    numerator  =: * denominator
> 
>    ] y=: %/ x: _1e9 + ?. 2 $ 2e9
> 905093854r670464245
>    denominator y
> 670464245
>    numerator y
> 905093854
> 
> 
> 
> ----- Original Message -----
> From: Brian Schott <[email protected]>
> Date: Tuesday, March 3, 2009 20:01
> Subject: [Jprogramming] deconstructing rationals
> To: [email protected]
> 
> >     How do you get the numerator and denominator of a
> > rational fraction? For example the (numerator,denominator)
> > of 4r6 is 2 3; how do you get the 2 3 from 4r6? I know this
> > must have been covered before, but I cannot think how to
> > search for it.
> > 
> >     My current best effort is the verb ratio.
> >    ratio =: 2{.!.1 (i.&'r' ( {./  , ' ' , ] }.~ 
> > #...@] <. >:@[) ])&.":
> >    ratio 4r6
> > 2 3
> >    ratio 4
> > 4
> >    $ratio 4
> > 
> >    $ratio 4r6
> > 2
> >    #ratio 4
> > 1
> >    #ratio 4r6
> > 2
> >    ratio"0 ]4r6 6r4 79
> >  2 3
> >  3 2
> > 79 1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to