Hi,
I am working through Project Euler to learn J. For PE 13 I am trying to
implement a carry function. For example:
41 carry 35 = 39 NB. Add the tens from 41 to the ones of 35
This is what I have so far:
NB. "Large" numbers
] T=: 6 6 $ 3 7 1 0 7 9 4 6 3 7 6 9 7 4 3 2 4 9 9 1 9 4 2 2 2 3 0 6 7 5
8 9 2 6 9 7
3 7 1 0 7 9
4 6 3 7 6 9
7 4 3 2 4 9
9 1 9 4 2 2
2 3 0 6 7 5
8 9 2 6 9 7
< \ S=: +/ |. "1 T NB. Suffixes of the reversed radix sums
+--+-----+--------+-----------+--------------+-----------------+
|41|41 35|41 35 25|41 35 25 18|41 35 25 18 30|41 35 25 18 30 33|
+--+-----+--------+-----------+--------------+-----------------+
f=: (0&{ @: ((0 10) & #:)) @: [ NB. Grab the number of tens in x
g=: +
h=: ]
carry=: (f g h)
carry/ L:0 < \ S
+--+--+--+--+--+--+
|41|39|32|27|40|46|
+--+--+--+--+--+--+
My carry verb works fine for the first two numbers, however as soon as
there are three or more numbers it looks like all the "tens" from x is
added to y, e.g. 4+3+25 = 32 instead of 28. My understanding is that `/`
acts like a fold, maintaining an accumulator as in +/ 2 3 4 5 = (((2+3) +
4) + 5) so that the left hand side is the result of the computation to
date. I'm clearly missing something important here.
Any comments and help is appreciated,
Lafras
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm