There are pen and paper approaches, but assuming a brute force, and function 
that answers basic first part of counting 1s in a number

 c1 =: 10&(1 +/@:= #.inv)"0

and if you almost always want the sum from a list of arguments then 

c2 =: +/@:c1


a brute force tacit iterative way of answering your final question is by 
tracking 2 numbers (next value to count 1s, cummulative sum so far)

 5 ( (0 >:@{ ]) , (1 { ]) + 0 c1@>:@{ ]) 10 2

11 4

Note the function is ambivalent while ignoring the right parameter.  It returns 
the pair of "last" number in presumptive sequence along with the cummulative 
sum of 1s in that sequence.

to iterate until the sum will match a provided right argument:

 203 ( (0 >:@{ ]) , (1 { ]) + 0 c1@>:@{ ])^:([ > 1 {:: ])(^:_) 1 1

511 204

keeps the pair number.  That might be useful to know that there is no sequence 
that has an exact 203 cummulative sum, but otherwise a function can lopp off 
that extra information:

203 ( (0 >:@{ ]) , (1 { ]) + 0 c1@>:@{ ])^:([ > 1 {:: ])(^:_)({.@) 1 1

511




On Monday, January 17, 2022, 02:19:38 p.m. EST, Skip Cave 
<s...@caveconsulting.com> wrote: 





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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to