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, -- Raul On Mon, Jan 17, 2022 at 7:42 PM Hauke Rehr <hauke.r...@uni-jena.de> wrote: > > This is a working solution. Not pretty, but it gets the job done. > > onesList =: (0<{.)`($:@}. + ((10 ^ <:@#) * 1<{.) + ((1 + 10 #. }.) * > 1={.) + <:@# * {. * 10 ^ 2 -~ #)@.(1<#) > ones =: 10 onesList@:(#.^:_1)"0 ] > NB. tests > ones 30 521 > 13 213 > > That was easy. Now for the “inverse:” > > dlog =: 10&^. > flog =: <.&.dlog > NB. no $: inside of DDs > dekasect =: {{ > 0 dekasect y;0;0;(* flog@<.@dlog) flog y > : > 'N offset start step' =: y > if. step < 1 do. offset return. end. > stips =. start + steps =. step * i. 11 > stops =. (x * steps) + ones stips > totals =. stops + ones offset - start > idx =. stops I. N > if. idx < # steps do. > if. N = idx { totals do. idx { stips return. end. > end. > (x + idx = 2) dekasect N; ((;~offset&+) (<:idx) { steps), < step % 10 > }} > > NB. test > dekasect 213 > 530 > > It doesn’t return the smallest/largest one but the earliest one found. > > That’s been fun. > > > Am 17.01.22 um 20:18 schrieb Skip Cave: > > If I want to find the number of '1' digits in an integer, I can design a > > verb c1, (count ones) to do that: > > > > *c1=.{{+/1=10#.^:_1]y}}"0* > > > > > > Test it: > > > > * c1 10* > > > > *1* > > > > * c1 11* > > > > *2* > > > > * c1 103416* > > > > *2* > > > > * c1 56161764121* > > > > *4* > > > > I can also find the total number of '1' digits in a list of sequential > > integers from 1 to n: > > > > > > * to=:[+i.@:>:@-~ * > > > > * +/c1 1 to n=.30* > > > > *13* > > > > * +/c1 1 to n=.521* > > > > *213* > > > > > > My question is: Given the total number of 1s in a sequence from 1 to n, > > what is n? > > > > A verb F(x) should return the final integer n, in the sequential list 1 to > > n, that contains x ones. > > > > > > Using the previous results as an example: > > > > > > * F 213 *NB. A sequence 1 to n has 213 ones. What is n? > > > > *521 *NB. The sequence 1 to 521 has 213 ones > > > > > > What are some options to design the verb F? > > > > 1) Explicit > > > > 2) Implicit > > > > 3) Iteration > > > > 4) Recursion > > > > Skip Cave > > Cave Consulting LLC > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > > -- > ---------------------- > 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