Re: [Jprogramming] Programming question

2022-01-23 Thread Hauke Rehr
golfing a bit more, just for fun (but it may improve performance a tiny bit, too) ones1=: (1(,,<,=){.) +/@:* (0:`$:@.(0<#),X)@}. where XX…XX is either of (*10^<:)@#,(10^#),1+10#.] (,&1*10^(,~<:))@#,1+10#.] (,&1*10^_1 0&+)@#,1+10&#. I thought I would squeeze more than 1 s

Re: [Jprogramming] Programming question

2022-01-23 Thread Raul Miller
Yeah, but I was just waking up and did not read Cliff's message carefully enough. Here's what I think he was after: c1=: {{+/1=10#.^:_1]y}}"0 F=: {{{.N#~y=+/\c1 N=.>:i.10>.5*y}} F 213 521 This uses his proposed components, achieves what he asked for, and eliminates the 'n' which would make F

Re: [Jprogramming] Programming question

2022-01-23 Thread Hauke Rehr
ones1 reads better than the original. factoring out {. and }. really helps. even though it means pairs of factors are “distributed” and one doesn’t immediately see which one is corresponding. if it hadn’t been for having it fit on one line, I had kept the recursive call separate, though. Am 23.01

Re: [Jprogramming] Programming question

2022-01-23 Thread Jan-Pieter Jacobs
Hi Skip, When using n (or v) as names in a direct definition, it assumes you're defining a conjunction: F=.{{{.n#~y=+/\c1 n=.>:i.100}} F 19 F(19) type <'F' ┌───┐ │conjunction│ └───┘ FF=:{{{.nn#~y+/\c1 nn=.>:i.100}} type<'FF' ┌┐ │verb│ └┘ or you can explici

Re: [Jprogramming] Programming question

2022-01-23 Thread Raul Miller
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 Hauk

Re: [Jprogramming] Programming question

2022-01-22 Thread Skip Cave
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 wrote: > Thanks for all the examples. I thought that the solution would be pretty > straightforward. > > c1 gives the number of ones in a

Re: [Jprogramming] Programming question

2022-01-22 Thread Skip Cave
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 ru

Re: [Jprogramming] Programming question

2022-01-18 Thread Raul Miller
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.

Re: [Jprogramming] Programming question

2022-01-17 Thread Hauke Rehr
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 measu

Re: [Jprogramming] Programming question

2022-01-17 Thread 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 "

Re: [Jprogramming] Programming question

2022-01-17 Thread Hauke Rehr
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:” dl

Re: [Jprogramming] Programming question

2022-01-17 Thread 'Pascal Jasmin' via Programming
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 yo

Re: [Jprogramming] Programming question

2022-01-17 Thread Elijah Stone
Interesting problem. 1s in 1 to 9: 1 1s in 10 to 99: 20 = 10 + 10*1 1s in 100 to 999: 300 = 100 + 10*20 1s in 1000 to : 4000 = 1000 + 10*300 1s in the naturals below 10^p: (p-1)*10^p-1 But I am fairly certain this does not apply for non-integral p. Not sure if a closed-form solution is po