[Jprogramming] Interesting Quora Problem:
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
Re: [Jprogramming] Interesting Quora Problem:
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 ( 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
Re: [Jprogramming] Interesting Quora Problem:
If we do a bit of work by ourselves? Consider that the 5 has to be in fifth position (starting from 1), and that there must be even digits (four of them) on the even positions. The other four odd digits go to the remaining odd positions. Then there are only 576 possibilities left. odds =: (i.24) A. '1379' NB. there's a better trick, is there? evens =: (i.24) A. '2468' all =: ,/ odds {{ }. x 1 3 7 9 } y 2 4 6 8 } '0oeoe5eoeo' }}"1/ evens all #~ (1+i.9) {{*./ 0 = x | ". x {."0 1 y }}"1 all 381654729 Direct Definition rules! Greetings, Ben On Mon, 11 Sept 2023 at 08:09, Raul Miller 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 (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 > 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
Re: [Jprogramming] Interesting Quora Problem:
Wow! Raul & Ben both provided solutions to my Quora problem that were radically different from my original solution, and radically different from each other. Both solutions reduced the execution time & space by several orders of magnitude, when compared with my original solution. Also, both used different approaches that took advantage of features in J which I knew about, but had not used very much. A good learning experience. 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 (^:)? Ben used the Anagram primitive (A.) to generate permutations of digits. Now I'm still trying to get my head around (parse) the two direct definitions Ben provided: all =: ,/ odds {{ }. x 1 3 7 9 } y 2 4 6 8 } '0oeoe5eoeo' }}"1/ evens all #~ (1+i.9) {{*./ 0 = x | ". x {."0 1 y }}"1 all If explained in detail, this example would make a good tutorial on how to use Direct Definition. This Quora problem shows the versatility of J primitives, and how J can provide multiple tools for different approaches to solve a specific problem. Skip Cave Cave Consulting LLC On Sun, Sep 10, 2023 at 1:01 PM Skip Cave 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