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
Hi Lafras,
It sounds like you don't want the whole sum of the carry's just the sum of the
last two in each group. When I use your S as you have defined it I get
S
36 35 26 16 22 32
then I take the vector overlapping 2 at a time to make a table
2 ]\ S
36 35
35 26
26 16
16 22
22 32
then
Hi Bob,
Thanks for your reply. For the sequences,
|41|41 35|41 35 25|41 35 25 18|41 35 25 18 30|41 35 25 18 30 33|
the expected output is
|41|39|28|20|32|36|
so that when you read 36 and all the "ones" in reverse, i.e. 3620891, you
get the sum of the large numbers, as in,
+/ 10 #. "1 T
36
Here's my take on what I think you are asking for:
10.inv 41 35
4 1
3 5
+//. 10.inv |.41 35
3 9 1
2 {. +//. 10.inv |.41 35
3 9
(2 {. +//.)&. (10.inv) |.41 35
39
carry=: (2 {. +//.)&. (10.inv)@|.
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
+/
Interesting that my solution only differs from yours in the fifth box where I
get 31 instead of 32, but irrelevant since it is not correct.
Looking at your original question you wrote
+/ 2 3 4 5 = (((2+3) + 4) + 5)
but actually J would evaluate right to left.
+/ 2 3 4 5 = (2 + (3 + (4 + 5)))
Hi,
Thanks Bob, that was the problem. The following works:
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=: +/ T NB. Suffixes of the radix sums
While your approach is certainly achieving your objective of helping you
learn the ins and outs of J, it doesn't seem to be the simplest or quickest
way to solve the PE13 problem.
Maybe worthwhile taking a step back?
On Mon, May 22, 2017 at 9:50 PM, Lafras Uys wrote:
> Hi,
>
> I am working thro