If you are working with a limited range, you can use I. to conduct the search.
For example, ones1=: (1,1&<,1&=,])@{. +/@:* (0:`$:@.(0<#),(10^#),(1+10#.]),#*10^#-1:)@}. ones=: 10 ones1@:(#.inv)"0 ] senO=: ] I.~ [: +/\1+/"1@:=10 #.inv [: i. +:@]^:(> ones)^:_~ Here, I used a golfed version of Hauke Rehr's 'ones' verb in an estimator for a bound on the list length that I needed to search (I shortened it to avoid email line wrap issues). But we could use something else to perform the estimate. The limitation here is that we have to form an intermediate result large enough to search. So you start getting noticeable lag around 3e5 and you probably don't want to go much beyond 1e6 very often. I hope this helps, -- Raul On Sun, Jan 23, 2022 at 1:03 AM Skip Cave <s...@caveconsulting.com> wrote: > > Also, > > Also, why doesn't this work: > > F=.{{{.n#~y=+/\c1 n=.>:i.100}} > > F 19 > > F(19) > > > > > Skip Cave > Cave Consulting LLC > > > On Sat, Jan 22, 2022 at 11:50 PM Skip Cave <s...@caveconsulting.com> wrote: > > > Thanks for all the examples. I thought that the solution would be pretty > > straightforward. > > > > c1 gives the number of ones in an integer: > > * c1=.{{+/1=10#.^:_1]y}}"0* > > > > > > Test it: > > > > * c1 211* > > > > *2* > > > > > > So the number of ones from 1 to 81 is: > > > > * +/c1 i.>:81* > > > > *19* > > > > To develop the inverse verb F, we can get a running sum of ones: > > * +/\c1 >:i.100* > > > > * 1 1 1 1 1 1 1 1 1 2 4 5 6 7 8 9 10 11 12 12 13 13 13 13 13 13 13 13 13 > > 13 14 14 14 14 14 14 14 14 14 14 15 15 15 15 15 15 15 15 15 15 16 16 16 16 > > 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 18 18 18 18 18 18 18 18 18 > > 18 19 19 19 19 19 19 19 19 19 19 20 20 20 20 20 20 20 20 20 21* > > > > > > So to find the integer where the sum of ones equals 19: > > > > *F=.3 :'{.n#~y=+/\c1 n=.>:i.100'* > > > > * F 19* > > > > *81* > > > > This works great, except that I need to make sure that the running sum > > index n, is larger than the expected answer. In this case, the integers > > from one to 100 were sufficient, but that was discovered by trial and error. > > > > > > Ideally, the running sum could be defined as an iterative process that > > terminates in an integer, when the running sum iteration reaches or exceeds > > the required count. Then the output of F is that integer. > > > > > > So I was mainly wanting to see how to define a verb F that iterates on a > > running sum of one counts until the sum reaches the right argument of F, > > then returns the integer that terminates the ones count. > > > > So I just want to know how to modify: > > > > *F=.3 :'{.n#~y=+/\c1 n=.>:i.100'* > > > > to a running-sum iteration which terminates & returns the final integer > > that matches the ones count input, and does not have to predefine the index > > count. > > > > Skip Cave > > Cave Consulting LLC > > > > > > On Tue, Jan 18, 2022 at 3:58 AM Raul Miller <rauldmil...@gmail.com> wrote: > > > >> I should have removed from 'seno' the line that says: > >> assert. hi<1000 > >> > >> The purpose of that line was to save me from having to use jbrk if I > >> hit an infinite loop from getting my conditions backwards. It's not a > >> useful mechanism, and should have been removed before I posted the > >> implementation. > >> > >> FYI, > >> > >> -- > >> Raul > >> > >> On Tue, Jan 18, 2022 at 2:27 AM Hauke Rehr <hauke.r...@uni-jena.de> > >> wrote: > >> > > >> > I didn’t do it much differently, conceptually. > >> > But instead of binary search (bisection) > >> > I split it up in ten buckets (dekasect). > >> > Apologies for the bad naming: > >> > another greek-latin hybrid, oh my … > >> > > >> > I split that way because I wanted to evaluate > >> > ones more often on small inputs than large. > >> > Didn’t measure if it atcually helps, though. > >> > And maybe I should have added an M. in order > >> > to memoize? > >> > > >> > But seno definitively reads better. > >> > > >> > Am 18.01.22 um 02:54 schrieb Raul Miller: > >> > > Your 'ones' is a bit more efficient than the routine I had come up > >> > > with, so I'll stick with your implementation here. > >> > > > >> > > Meanwhile, it's perhaps worth noting > >> > > > >> > > I.213=ones i.1000 > >> > > 521 522 523 524 525 526 527 528 529 530 > >> > > > >> > > So, building the inverse... it's typical for there to be ten numbers > >> > > who had n "one digits in the sequence". But, for example, there's no > >> > > number which has exactly 3 "one digits in the sequence". Ten has 2, > >> > > eleven has four. But we can find the minimum value which had at least > >> > > n preceding one digits. > >> > > > >> > > Personally, I'd use a binary search here: > >> > > > >> > > seno=:{{ > >> > > if.y=ones y do.y return.end.hi=.2*lo=.y > >> > > while.y>ones hi do. hi=.2*lo=.hi end. > >> > > while.hi>lo do. > >> > > assert. hi<1000 > >> > > select.*y-ones mid=.<.-:lo+hi+1 > >> > > case._1 do.if.hi=mid do.lo=.hi break.end.hi=.mid > >> > > case.0 do.lo=.hi=.mid break. > >> > > case.1 do.lo=.mid > >> > > end. > >> > > end. > >> > > while.y <: ones lo do.lo=.<:hi=.lo end. NB. maybe too crude... > >> > > hi > >> > > }} > >> > > > >> > > seno 213 > >> > > 521 > >> > > ones 521 > >> > > 213 > >> > > > >> > > FYI, > >> > > > >> > > >> > -- > >> > ---------------------- > >> > mail written using NEO > >> > neo-layout.org > >> > > >> > ---------------------------------------------------------------------- > >> > 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