On Thu, Jul 23, 2009 at 9:35 AM, Dan Bron<[email protected]> wrote: > Implement Ethiopian multiplication in J, as concisely as possible (no, * > doesn't count). Here's my tee-off:
Note that you can also use <....@-:^:a: This halts when you reach zero, and zero is an even number, which means it will be discarded. Note that you do not need the values from the number which you are halving -- you only need the remainders. This leaves us with (2 | [: i...@# <....@-:^:a:) If we want real conciseness, we could replace this with (0 , #:) Or, since the leading zero is irrelevant (and was not an official part of the original algorithm): #: This leaves us with doubling, selection and adding. The doublings could be accomplished in a variety of ways. For example. +:@]^:(i...@[) However, if multiplication by the binary representation is allowed, then another approach seems appealing emul=: p.&2@([ * |....@#:@]) 13 emul 23 299 FYI, -- Raul > > ethiop =: +/@:( (2&|@:] # (*2^i.@:#)) (1>.<.@:-:)^:a: )~ > > EG: > > 17 ethiop 34 > 578 > > 17 * 34 > 578 > > -Dan > > [1] http://rosettacode.org/wiki/Ethiopian_Multiplication > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
