Thank you. Correct.
The theme I was proud to contribute was J's excellent tools for "successive"
(same family as recursive) algorithms. Insert/Fold being used here. A concept
I've brought up incidentally on previous occasions is that of a "Perfect
Function"... defined as a function that returns a result of compatible shape to
its primary (y) argument, and might meaningfully be called again on that
argument. These can be use with J's ^: / F. ] function family for loopless
functional code. Raul's F function applied repeatedly inspired my Fold
replacement. It was already an improvement over searching through all
permutations.
This does improve timings
BASE10DIVbyINDEX =: '';1 3 7 9; 2 4 6 8;1 3 7 9; 2 4 6 8;5;2 4 6 8;1 3 7 9;2
4 6 8;1 3 7 9
v2 =: ([ (] #~ [ = 10&#.inv(#@~."1@:)@]) [ (] #~ 0 = |) (BASE10DIVbyINDEX {::~
[) +("1 0)(,@:) 10 * ])
I =: ](F.:)
10 timespacex ' v2/ 9 8 7 6 5 4 3 2 1 0'
5.671e_5 7136
10 timespacex ' 1 3 7 9 v2 I 9 8 7 6 5 4 3 2'
7.71e_5 9952
though the single filter operation outperforms "successive pruning"
v3 =: ([ (] #~ 0 = |) (BASE10DIVbyINDEX {::~ [) +("1 0)(,@:) 10 * ])
10 timespacex '(#~ 9 = 10&#.inv(#@~."1@:)) (1 3 7 9) v3 I 9 8 7 6 5 4 3 2'
4.305e_5 19840
timespacex '(#~ 9 = 10&#.inv(#@~."1@:)) v3/ |. i.10'
3.11e_5 17152
On Wednesday, September 13, 2023 at 02:30:11 p.m. EDT, Jo van Schalkwyk
<[email protected]> wrote:
You do know that (>:i.9) can be replaced by 1 3 7 9, because there are just
4 even digits and 4 places for them, so the rest must be odd (and 5 is
taken)?
Dr Jo.
On Thu, 14 Sept 2023 at 04:14, 'Pascal Jasmin' via Programming <
[email protected]> wrote:
> A variation that excludes 0s, and prunes after each "iteration"
>
> (>:i.9) ([ (] #~ [ = 10&#.inv(#@~."1@:)@]) [ (] #~ 0 = |) (>: i.9) +("1
> 0)(,@:) 10 * ])I 9 8 7 6 5 4 3 2
>
> 381654729
>
>
> 10 timespacex '(>:i.9) ([ (] #~ [ = 10&#.inv(#@~."1@:)@]) [ (] #~ 0 = |)
> (>: i.9) +("1 0)(,@:) 10 * ])I 9 8 7 6 5 4 3 2'
>
> 0.00016005 33664
>
> twice as long, but low space.
> As an experiment for whether working to cut down additions (and filter
> universe), and "compiling a lookup table into a verb" provides a
> performance improvement:
> BASE10DIVbyINDEX =: '';(>:i.9); 2 4 6 8;(>:i.9); 2 4 6 8;5;2 4 6
> 8;(>:i.9);2 4 6 8;(>:i.9)
> v =: ([ (] #~ [ = 10&#.inv(#@~."1@:)@]) [ (] #~ 0 = |) (BASE10DIVbyINDEX
> {::~ [) +("1 0)(,@:) 10 * ])I
>
>
> 10 timespacex '(>:i.9) v 9 8 7 6 5 4 3 2'
> 0.00015283 16864
> to test a / version
> v2 =: ([ (] #~ [ = 10&#.inv(#@~."1@:)@]) [ (] #~ 0 = |) (BASE10DIVbyINDEX
> {::~ [) +("1 0)(,@:) 10 * ])
>
>
>
>
> 10 timespacex 'v2/ 9 8 7 6 5 4 3 2 1 0'
>
> 0.00013159 14496
>
>
>
> One conclusion is that a single large filtering operation is more time
> efficient than repeated filtering to save space,and search size.
> On Tuesday, September 12, 2023 at 08:17:54 p.m. EDT, 'Pascal Jasmin'
> via Programming <[email protected]> wrote:
>
> lower code version, of Raul's.
> I may not have understood problem, but I get 2492 solutions (oops... now I
> understand that each digit needs to be used just once.
> I =: ]F.: NB. insert using fold for shape flexibility
>
> #(>:i.9) ([ (] #~ 0 = |) (i.10) +("1 0)(,@:) 10 * ])I 9 8 7 6 5 4 3 2
>
>
> # (>:i.9) ([ (] #~ 0 = |) (i.10) +("1 0)(,@:) 10 * ])I 9 8 7 6 5 4 3 2
> 2492
>
> 5 {. (>:i.9) ([ (] #~ 0 = |) (i.10) +("1 0)(,@:) 10 * ])I 9 8 7 6 5 4 3 2
>
> 102000564 102006162 102006801 102006882 102054402
>
> with spec, but 0 allowed to be included in distinct digits
>
> (#~ 9 = 10&#.inv(#@~."1@:)) (>:i.9) ([ (] #~ 0 = |) (i.10) +("1 0)(,@:)
> 10 * ])I 9 8 7 6 5 4 3 2
>
> 381654720 381654729 783204165 801654723
>
> only 1 above doesn't include 0 (the 1 solution sought)
>
> timespacex '(#~ 9 = 10&#.inv(#@~."1@:)) (>:i.9) ([ (] #~ 0 = |) (i.10)
> +("1 0)(,@:) 10 * ])I 9 8 7 6 5 4 3 2'
>
> 0.000899 603168
>
>
> On Sunday, September 10, 2023 at 06:09:30 p.m. EDT, Raul Miller <
> [email protected]> wrote:
>
> I guess to simplify it, I would filter the possibilities at each step.
>
> first digit divisible by 1:
> N1=: 1+i.9
>
> Second digit divisible by 2:
> N2=: ;(10*N1)+each (<2*1+i.4)-.each N
>
> Third digit divisible by 3:
> digits=: 10&#.inv
> N3=: (#~ 0=3|]);(10*N2)+each (<1+i.9) (-. digits) each N2
>
> And, so on... and since we're basically doing the same thing at each
> step, we could encapsulate and parameterize this step as a function.
>
> F=: {{(#~ 0=x|]);(10*y)+each (<N1) (-. digits) each y}}
> 9 F 8 F 7 F 6 F 5 F 4 F 3 F 2 F N1
> 381654729
>
> timespacex '9 F 8 F 7 F 6 F 5 F 4 F 3 F 2 F N1'
> 0.0006679 38400
>
> So that took about a millisecond of machine time on this little laptop.
>
> Further simplifications are possible, but since this is a one use
> calculation this is probably good enough.
>
> --
> Raul
>
> On Sun, Sep 10, 2023 at 2:02 PM 'Skip Cave' via Programming
> <[email protected]> wrote:
> >
> > Quora Question: Can you arrange the digits 1-9 to make a nine-digit
> number
> > such that the first digit is divisible by one, the first two digits are
> > divisible by two, the first three divisible by three and so on?
> >
> > My solution:
> > ea=.&.>
> >
> > at=.>10#.ea(a=.362880 9$1 to 9){.ea{n=.>:perm 9
> >
> > {:"1 at#~*./"1[0=a|"1 at
> >
> > 381654729
> >
> >
> > The answer is 381654729.
> >
> >
> > Check:
> >
> > ]m=.>10#.ea(;/1 to 9){.ea{sep 381654729
> >
> > 3 38 381 3816 38165 381654 3816547 38165472 381654729
> >
> > (1 to 9)| m
> >
> > 0 0 0 0 0 0 0 0 0
> >
> >
> > There is only one answer. Is there a way to simplify or minimize the big
> > repetitive array 'a' to make the two J code lines more memory efficient &
> > succinct?
> >
> >
> > 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
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
>
> | | Virus-free.www.avast.com |
>
> ----------------------------------------------------------------------
> 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