> From: Lau B. Jensen > > #6: Difference between Sum of the squares and square of the sum > (from the ints 1 - 100) > > This one really had me eating my words regarding combining functions > for a while. Until I discovered [: cap, I struggled to make this > simple algo run. I even tried naming the components to no avail. > Eventually I ended up with > > (([:*:+/)-([:+/*:)) >:i.100
Nothing wrong with this - I know I originally found Cap easier to deal with than At and Atop. I think it would be better to add some spaces between the primitives though to make the forks clearer. (([: *: +/) - ([: +/ *:)) >: i.100 The bit that may have been tripping you up using At (@:) is the difference between ( *:@:+/ ) and ( *:@:(+/) ). The first one is equivalent to ( (*:@:+)/ ) which is not what you want. (*:@:(+/) - +/@:*:) >:i.100 > #8: Find the greatest product of 5 consecutive numbers in a 1000-digit > number > > This was a bit disappointing for me, as I thought I could solve it > straight off, but quickly > ran into all sorts of trouble. > > I started out just working with a subset of the entire number > > data=: ": 73167176531330624919225119674426574742355349194934x > > With that as a string representation, I figured that partitioning it > would be simple > > 5<\ data > +-----+-----+-----+-----+-----+ > |73167|31671|16717|67176|71765|....... > +-----+-----+-----+-----+-----+ > > And it was. So all thats left is calculating the product of the > individual cells. > Which I had hoped was something like > > */\"0 (5<\ data) > > But for some reason that just adds a leading axis and does nothing to > the individual cells > > (*/\"0)"0 ,(5<\ data) > +-----+ > |73167| > +-----+ > |31671| > +-----+ > |16717| > +-----+ > |67176| > +-----+ > ... > > So my question might essentially be narrowed down to this: How do I > apply */ to the digits of each window? You can use "each" (&.>) */ each 5<\data Or */ &.> 5<\data However currently the data in the boxes is character (not numeric) so you're going to have to convert it to numeric as well. ([: */ _&"."0) &.> 5<\ ": data +---+---+---+----+----+ |882|126|294|1764|1470| +---+---+---+----+----+ Boxing is usually best saved for arrays of unequal length. Here all the lists are the same length so it is not necessary. 5 ]\data 73167 31671 16717 67176 71765 ... (_&"."0) 5 ]\data 7 3 1 6 7 3 1 6 7 1 1 6 7 1 7 6 7 1 7 6 7 1 7 6 5 ... */"1 (0&"."0) 5 ]\data 882 126 294 1764 1470 However it is probably neater to work directly with the groups of 5. 5 ([: */ _&"."0)\ data 882 126 294 1764 1470 ... Or even better still convert them back to integers before grouping: data=: _&"."0 ": 73167176531330624919225119674426574742355349194934x 5 */\ _"."0 ": data 882 126 294 1764 1470 ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
