On May 5, 2012, at 12:01 PM, Raul Miller <rauldmil...@gmail.com> wrote:
> On Fri, May 4, 2012 at 6:36 PM, Joe Groff <arc...@gmail.com> wrote: >> Thanks! I was trying to cook something up like that myself. Here's a >> tweaked version that works with heterogeneous ranks: >> >> --- >> powmod2 =: 1 :(':';'(m|*)/(|.(i.<:#$b)|:b)^~(m|*:)^:(<{:$b=.#:y) x') >> 5 6 7 (10 powmod2) 3 2 $ 40 0 0 41 42 0 >> 5 1 >> 1 6 >> 9 1 > > There's a simpler way to accomplish that: > > 5 6 7 (10&|@^) 3 2 $ 40 0 0 41 42 0 > 5 1 > 1 6 > 9 1 Sorry, that was a dumb example. As I said in the original post, m&|@:^ doesn't work when m is a list. > They can be generalized rather easily, in my opinion, but > before trying to generalize it's usually better to define > what it is you are trying to accomplish. I was trying to write an array-based implementation of the Miller-Rabin primality test that worked for both a single candidate and a list of candidates. (I know that the algorithm should short circuit, and an array-based implementation does extra unnecessary work; this was just a learning J exercise.) The obvious (to me) way to do that was with an operator that could handle both rank0 | rank2 ^ rank1 and rank1 | rank3 ^ rank2 operations in the natural way while still using modular exponentiation. -Joe ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm