NB. Oh!  j implements antibase as
   NB. "insert divmod" from right to left.

   COINS =: 2 5r2 2 5

   Dyad =: [: :
   divmod =: foo =: <.@:%~ , |
   antibaseJ =: }.@:(((divmod {.) , }.@:])/@:,)"1 0 Dyad

   COINS antibaseJ 38
1 1r2 1 3


   NB. I had envisioned a left-to-right algorithm

   antibaseDWL =: finish@:work@:prepare"1 0 Dyad
   moddiv =: | , <.@:%~
   prepare =: (,~ (1 , |.@:(*/\.)@:}.))~
   work =: ((moddiv {.@:]) , }.@:])/
   finish =: |.@:}.

   COINS prepare 38
1 5 10 25 38

   work 1 5 10 25 38
0 3 0 1 1

   finish 0 3 0 1 1
1 1 0 3

   COINS antibaseDWL 38
1 1 0 3

Date: Sat, 25 Jan 2014 21:53:28 -0800
From: Nollaig MacKenzie<[email protected]>
To:[email protected]
Subject: Re: [Jprogramming] #: antibase
Message-ID:<[email protected]>
Content-Type: text/plain; charset=us-ascii


On 2014.01.25 20:33:55, you,
  the extraordinary David Lambert, spake thus:

>    NB. convert 38 cents to pennies, nickels, dimes, quarters.
>
>    38 #:~ COINS =: |. 1 5 2 5r2 2
>1 1r2 1 3 0
>
>    NB. why do I get a half dime and a nickel?
>
>    COINS #. 1 1 0 3 0
>38
>
foo=. ((<.@:(%~)),|)
C=. 2 5r2 2 5


    5 foo 38
7 3
    2 foo 7
3 1
    5r2 foo 3
1 1r2
    2 foo 1
0 1

and the result of C #: 38 (tracing upward) is 1 1r2 1 3

    C#.1 1r2 1 3
38


-- Nollaig MacKenzie http://www.yorku.ca/nollaig

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

Reply via email to