Raul,
That's nice! I'm not expert enough in J to come up with that (+/\ 7&|)
construct on my own, so I did effectively the same thing in my solution,
with lots more code. I'm going to have to study hooks and conjunctions
some more, to be able to put that all together on my own.
The real insight is that you used the same set of integers for the
consecutive sequences as well as the starting integers of each sequence.
That concept shrinks the code considerably. 1000 terms was way overkill, as
60 terms would do, but it's better to be safe than sorry. Machines are big
enough these days that a 1K x 1K array is nothing.
The approach I took ended up giving me more interesting information about
all the sequences, but wasn't optimum for just answering the question.
Find the mask that selects all the sequences that sum to 156:
*m=.0<ix=.,I.156=+/\"1]7|(0 to 40)+/1 to 100*
Use the mask to find all the beginning integers of each sequence that will
sum to 156.
*]bg=.m#1 to 41 *
*2 4 6 9 11 13 16 18 20 23 25 27 30 32 34 37 39 41 *
Use the mask to find all the indices where the sequence summed to 156:
*]ct=.1+m#ix *
*52 51 53 52 51 53 52 51 53 52 51 53 52 51 53 52 51 53*
So the answer to the question was just:
*~.ct*
*52 51 53*
This approach allowed me to find all the pairs of beginning integers in
each sequence, paired with the number of integers in that sequence which
meet the criteria. This wasn't part of the question, but the data i
generated made it easy to discover all the possible start integer/sequence
length pairs:
*{bg,.ct *— | beginning integer, count of terms |
*│2 52│4 51│6 53│9 52│11 51│13 53│16 52│18 51│20 53│23 52│25 51│27 53│30
52│32 51│34 53│37 52│39 51│41 53│*
With these pairs, I can generate all the sequences defined by each of
these begin/count pairs, divide everything by 7 getting the remainders, and
sum the remainders in each of the sequences defined by bg & ct, to see what
we get: (ea=. each)
*, >+/ea 7|ea bg+ea 18 1$i.ea ct*
*156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156 156*
Correct!
Skip Cave
Cave Consulting LLC
On Mon, Jan 14, 2019 at 1:08 AM Raul Miller <[email protected]> wrote:
> So, like this?
>
> I.+./|:156=(+/\ 7&|)i.1000
>
> 51 52 53
>
>
> Thanks,
>
>
> —
>
> Raul
>
> On Monday, January 14, 2019, 'Skip Cave' via Programming <
> [email protected]> wrote:
>
> > Ric, the problem that got me started thinking about the "find the index
> in
> > the vector/array where the value is x" issue, was a problem posed on
> Quora:
> >
> > The original question as posted: *They are considered to be consecutive
> > natural numbers. The sum of the remainders of those numbers at 7 is 156.
> > What are all the positive values of n?*
> >
> > That wasn't very clearly stated, so I re-phrased the problem to hopefully
> > make it more clear, at least to me: *n consecutive numbers are divided by
> > 7, and their remainders add to 156. What are all possible values of n? *
> >
> > My brute force solution to this problem was basically to generate
> multiple
> > consecutive sequences of integers, each starting at a new consecutive
> > starting integer. Make the sequences longer than required. Then find the
> > remainders of the numbers in each sequence after dividing by 7. Then find
> > the running sums of the remainders in each sequence. Finally I needed to
> > find the place (index), if any, where the running sum in each sequence
> was
> > equal to 156, which would tell me how long the sequence needed to be, to
> > have the remainders sum to 156. The final answer to the question would be
> > to define what are all the unique sequence lengths are that sum to 156.
> >
> > The URL to the Quora question, my answer, and other answers is:
> > https://goo.gl/Z3UJGF <https://goo.gl/Z3UJGF>
> >
> > Skip Cave
> > Cave Consulting LLC
> >
> >
> > On Sun, Jan 13, 2019 at 11:15 PM Ric Sherlock <[email protected]> wrote:
> >
> > > Basically you are seeing the result of monadic =
> > >
> > > l2
> > >
> > > 5 2 3 4 6 4 3 5 4 6 7 6
> > >
> > > = l2
> > >
> > > 1 0 0 0 0 0 0 1 0 0 0 0
> > >
> > > 0 1 0 0 0 0 0 0 0 0 0 0
> > >
> > > 0 0 1 0 0 0 1 0 0 0 0 0
> > >
> > > 0 0 0 1 0 1 0 0 1 0 0 0
> > >
> > > 0 0 0 0 1 0 0 0 0 1 0 1
> > >
> > > 0 0 0 0 0 0 0 0 0 0 1 0
> > >
> > > <@I. = l2
> > >
> > > ┌───┬─┬───┬─────┬──────┬──┐
> > >
> > > │0 7│1│2 6│3 5 8│4 9 11│10│
> > >
> > > └───┴─┴───┴─────┴──────┴──┘
> > >
> > >
> > > I think you can simplify your Idot to:
> > > Idot=: [: <@I. =
> > >
> > > It would be interesting to understand the actual problem you are trying
> > to
> > > solve with Idot.
> > >
> > >
> > > On Mon, Jan 14, 2019 at 5:56 PM 'Skip Cave' via Programming <
> > > [email protected]> wrote:
> > >
> > > > Ok. You have convinced me to go with the empty box as a null
> indicator.
> > > In
> > > > that case, we can make the Idot verb dyadic, and generalize it:
> > > >
> > > > Idot =.[:I.&.>[:{="1
> > > >
> > > > l1 =. 1 2 3 4 6 4 3 4 4 6 7 6
> > > >
> > > > 5 Idot l1
> > > >
> > > > ┌┐
> > > >
> > > > ││
> > > >
> > > > └┘
> > > >
> > > > l2 =. 5 2 3 4 6 4 3 5 4 6 7 6
> > > >
> > > > 5 Idot l2
> > > >
> > > > ┌───┐
> > > >
> > > > │0 7│
> > > >
> > > > └───┘
> > > >
> > > > ]m=.|:1 2 3 4,. 2 5 5 5,. 5 4 3 2 ,. 2 3 5 4,. 2 5 4 5
> > > >
> > > > 1 2 3 4
> > > >
> > > > 2 5 5 5
> > > >
> > > > 5 4 3 2
> > > >
> > > > 2 3 5 4
> > > >
> > > > 2 5 4 5
> > > >
> > > > 5 Idot m
> > > >
> > > > ┌┬─────┬─┬─┬───┐
> > > >
> > > > ││1 2 3│0│2│1 3│
> > > >
> > > > └┴─────┴─┴─┴───┘
> > > >
> > > > ]n=.|:2 5 5 5,. 1 2 3 4,. 5 4 3 2 ,. 2 3 5 4,. 2 5 4 5
> > > >
> > > > 2 5 5 5
> > > >
> > > > 1 2 3 4
> > > >
> > > > 5 4 3 2
> > > >
> > > > 2 3 5 4
> > > >
> > > > 2 5 4 5
> > > >
> > > > >m;n
> > > >
> > > > 2 5 5 5
> > > >
> > > > 1 2 3 4
> > > >
> > > > 5 4 3 2
> > > >
> > > > 2 3 5 4
> > > >
> > > > 2 5 4 5
> > > >
> > > > 1 2 3 4
> > > >
> > > > 2 5 5 5
> > > >
> > > > 5 4 3 2
> > > >
> > > > 2 3 5 4
> > > >
> > > > 2 5 4 5
> > > >
> > > >
> > > > 5 Idot > m;n
> > > >
> > > > ┌─────┬─────┬─┬─┬───┐
> > > >
> > > > │1 2 3│ │0│2│1 3│
> > > >
> > > > ├─────┼─────┼─┼─┼───┤
> > > >
> > > > │ │1 2 3│0│2│1 3│
> > > >
> > > > └─────┴─────┴─┴─┴───┘
> > > >
> > > >
> > > > NB. It's interesting what the monadic use of Idot does:
> > > >
> > > >
> > > > Idot l1
> > > >
> > > > ┌─┬─┬───┬───────┬──────┬──┐
> > > >
> > > > │0│1│2 6│3 5 7 8│4 9 11│10│
> > > >
> > > > └─┴─┴───┴───────┴──────┴──┘
> > > >
> > > > Idot l2
> > > >
> > > > ┌───┬─┬───┬─────┬──────┬──┐
> > > >
> > > > │0 7│1│2 6│3 5 8│4 9 11│10│
> > > >
> > > > └───┴─┴───┴─────┴──────┴──┘
> > > >
> > > > Idot m ┌─┬─────┬─┬─┐ │0│1 │2│3│ ├─┼─────┼─┼─┤ │0│1 2 3│ │ │
> > ├─┼─────┼─┼─┤
> > > > │0│1 │2│3│ ├─┼─────┼─┼─┤ │0│1 │2│3│ ├─┼─────┼─┼─┤ │0│1 3 │2│ │
> > > > └─┴─────┴─┴─┘
> > > >
> > > > Idot n
> > > >
> > > > ┌─┬─────┬─┬─┐
> > > >
> > > > │0│1 2 3│ │ │
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 3 │2│ │
> > > >
> > > > └─┴─────┴─┴─┘
> > > >
> > > > Idot >m;n
> > > >
> > > > ┌─┬─────┬─┬─┐
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 2 3│ │ │
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 3 │2│ │
> > > >
> > > > └─┴─────┴─┴─┘
> > > >
> > > >
> > > > ┌─┬─────┬─┬─┐
> > > >
> > > > │0│1 2 3│ │ │
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 │2│3│
> > > >
> > > > ├─┼─────┼─┼─┤
> > > >
> > > > │0│1 3 │2│ │
> > > >
> > > > └─┴─────┴─┴─┘
> > > >
> > > > Can you explain what's going on here?
> > > >
> > > >
> > > > Skip Cave
> > > > Cave Consulting LLC
> > > >
> > > >
> > > > On Sun, Jan 13, 2019 at 8:02 PM Ric Sherlock <[email protected]>
> > wrote:
> > > >
> > > > > Depending on what you are trying to acheive, I think I'd represent
> > the
> > > > lack
> > > > > of a match in a row as an empty rather than a _1:
> > > > >
> > > > > <@I."1 ] 5=m
> > > > >
> > > > > ┌┬─┬─┬─┐
> > > > >
> > > > > ││3│0│2│
> > > > >
> > > > > └┴─┴─┴─┘
> > > > >
> > > > >
> > > > > Of course if you need the _1 then you can transform the above
> > > > >
> > > > > ;@([: (a:=])`((<_1),:~ ])} <@I."1) 5 = m
> > > > >
> > > > >
> > > > > On Mon, Jan 14, 2019 at 10:42 AM 'Skip Cave' via Programming <
> > > > > [email protected]> wrote:
> > > > >
> > > > > > What I would really like is for I. to return a _1 whenever there
> is
> > > no
> > > > 1
> > > > > in
> > > > > > the match array, since there cannot be a negative index:
> > > > > >
> > > > > > I.5=1 2 3 4 6 4 3 4 4 6 7 6
> > > > > >
> > > > > > 4 8 10
> > > > > >
> > > > > > Idot 5=1 2 3 4 6 4 3 4 4 6 7 6
> > > > > >
> > > > > > _1
> > > > > >
> > > > > >
> > > > > > ]m=.|:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
> > > > > >
> > > > > > 1 2 3 4
> > > > > >
> > > > > > 2 3 4 5
> > > > > >
> > > > > > 5 4 3 2
> > > > > >
> > > > > > 2 3 5 4
> > > > > >
> > > > > > 5=m
> > > > > >
> > > > > > 0 0 0 0
> > > > > >
> > > > > > 0 0 0 1
> > > > > >
> > > > > > 1 0 0 0
> > > > > >
> > > > > > 0 0 1 0
> > > > > >
> > > > > > ,Idot .5=m
> > > > > >
> > > > > > _1 3 0 2
> > > > > >
> > > > > >
> > > > > > Can a verb Idot be designed, that does this?
> > > > > >
> > > > > > Skip Cave
> > > > > > Cave Consulting LLC
> > > > > >
> > > > > >
> > > > > > On Sun, Jan 13, 2019 at 2:41 PM Henry Rich <[email protected]
> >
> > > > wrote:
> > > > > >
> > > > > > > Right. Prefer (I.@:= ,) to I.@,@:= since it uses special
> code.
> > > > > > >
> > > > > > > Henry Rich
> > > > > > >
> > > > > > > On 1/13/2019 2:54 PM, 'Mike Day' via Programming wrote:
> > > > > > > > You often see this sort of thing, returning pairs of indices
> of
> > > all
> > > > > > > occurrences:
> > > > > > > >
> > > > > > > > 5 ($@] #.inv I.@,@:=) |: 1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,.
> 2
> > 3
> > > 5
> > > > 4
> > > > > > > > 1 3
> > > > > > > > 2 0
> > > > > > > > 3 2
> > > > > > > >
> > > > > > > > You can obviously get the row indices using {:”1 or some
> such,
> > > and
> > > > > you
> > > > > > > can of course make the bracketed code a named dyadic verb,
> > > > > > > >
> > > > > > > > Cheers,
> > > > > > > >
> > > > > > > > Mike
> > > > > > > >
> > > > > > > >
> > > > > > > > Sent from my iPad
> > > > > > > >
> > > > > > > >> On 13 Jan 2019, at 17:55, 'Skip Cave' via Programming <
> > > > > > > [email protected]> wrote:
> > > > > > > >>
> > > > > > > >> I know I can find the location (index) of a specific integer
> > in
> > > a
> > > > > > > vector of
> > > > > > > >> integers using I.
> > > > > > > >>
> > > > > > > >> I.5=1 2 3 4 5 4 3 4 5 6 5 6
> > > > > > > >>
> > > > > > > >> 4 8 10
> > > > > > > >>
> > > > > > > >>
> > > > > > > >> So I want to find the row index of a specific integer in an
> > > array
> > > > of
> > > > > > > >> integers:
> > > > > > > >>
> > > > > > > >> |:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
> > > > > > > >>
> > > > > > > >> 1 2 3 4
> > > > > > > >>
> > > > > > > >> 2 3 4 5
> > > > > > > >>
> > > > > > > >> 5 4 3 2
> > > > > > > >>
> > > > > > > >> 2 3 5 4
> > > > > > > >>
> > > > > > > >> 5=|:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
> > > > > > > >>
> > > > > > > >> 0 0 0 0
> > > > > > > >>
> > > > > > > >> 0 0 0 1
> > > > > > > >>
> > > > > > > >> 1 0 0 0
> > > > > > > >>
> > > > > > > >> 0 0 1 0
> > > > > > > >>
> > > > > > > >> ,I. 5=|:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
> > > > > > > >>
> > > > > > > >> 0 3 0 2
> > > > > > > >>
> > > > > > > >>
> > > > > > > >> The first zero indicates that there is no 5 in the first
> row.
> > > The
> > > > > > second
> > > > > > > >> zero gives the index of the 5 in the third row. How can I
> tell
> > > > > whether
> > > > > > > the
> > > > > > > >> zero is an index, or a null indicator?
> > > > > > > >>
> > > > > > > >> Skip
> > > > > > > >>
> > > > > ------------------------------------------------------------
> > ----------
> > > > > > > >> For information about J forums see
> > > > > > http://www.jsoftware.com/forums.htm
> > > > > > > >
> > > > > ------------------------------------------------------------
> > ----------
> > > > > > > > For information about J forums see
> > > > > http://www.jsoftware.com/forums.htm
> > > > > > >
> > > > > > >
> > > > > > > ---
> > > > > > > This email has been checked for viruses by AVG.
> > > > > > > https://www.avg.com
> > > > > > >
> > > > > > >
> > > >
> ----------------------------------------------------------------------
> > > > > > > For information about J forums see
> > > > http://www.jsoftware.com/forums.htm
> > > > > >
> > > ----------------------------------------------------------------------
> > > > > > For information about J forums see
> > > http://www.jsoftware.com/forums.htm
> > > > > ------------------------------------------------------------
> > ----------
> > > > > For information about J forums see http://www.jsoftware.com/
> > forums.htm
> > > >
> ----------------------------------------------------------------------
> > > > For information about J forums see
> http://www.jsoftware.com/forums.htm
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm