I wasn't going to comment - I'd worked up a clunky version for my own
amusement,
but not very interesting... but it does use power, ^: . So here goes:
(I later incorporated Ben Gorte's wheeze for limiting new digits to odd
or even,
though I didn't force the 5.)
NB. Given a partial answer, append extra suitable digits.
app=:{{
if. 10=nd =. >: #":{.y do. y return.
elseif. 2 = nd do.
y =.1 3 5 7 9 end.
par=. 2|<:{:y NB. plagiarising Ben Gorte's idea re odds & evens!
q =. q#~(=<.) nd%~ q=. ,(par+2*i.5) +/ 10*y
q#~(-:~.)@":"0 q
}}
Its time & space performance is ok:
app^:_] 0
381654729
ts'app^:_] 0'
0.0003165 16480
But I also came up with this loopy version - essentially the same as app
- which I rather prefer:
day =: {{) v
r =. odds =. >:+:i.5
for_w. 2 + i.8 do.
NB. get all width w divisible by w
r =. w (]#~ (=<.)@%~) ,(odds - 0 = 2 | w) +/ 10 * r
NB. OR r =. w (]#~ 0 = |) ,(odds - 0 = 2 | w) +/ 10 * r NB. less
time, more space
NB. ensure no repeated digits
r =. (#~ (-:~.)@":"0) r
NB. OR r =. (#~ (-:~.)@(10&#.inv)"0) r NB. similar time, more space
end.
}}
day 0
381654729
ts'day 0'
0.0005006 7904
Counting Angels on Pins, I know!
Mike
On 11/09/2023 14:03, Raul Miller wrote:
On Mon, Sep 11, 2023 at 2:48 AM 'Skip Cave' via Programming
<programm...@jsoftware.com> wrote:
Raul used a 'chain of dyadic verbs' (x F x F x F y) to solve the problem. I
haven't ever tried this, where right-to-left execution is used to feed the
result of one dyadic verb into the next one on the left. It was a bit
confusing until I realized that the rightmost verb 'eats' its left
argument, so that the verb result is passed to the next verb on the left,
instead of the previous verb's left argument. This problem provides a good
example of the use of this feature. I wonder if this linear 'chain of
verbs' could be replaced with the hatco operator (^:)?
You could use (^:) if you could derive the left argument from the
right argument.
For example, replacing (x) with (#digits y).
If that had not been possible, you could have used (/) instead of (^:)
but that forces you to box y and the individual x values so that they
could exist in a list together (and then you have to unbox them when
using them). Alternatively you could have used (]F.:) which behaves
similarly but allows an initial value (which would be N1 here) as a
left argument rather than being the rightmost element of the array.
(But this is a hypothetical general case, because deriving the x
argument from the y argument is straightforward here.)
FYI,
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm