Actually, one of my favorite pastimes is to polish my J programming skills by reading through Quora problems, looking at the one that have lengthy answers using esoteric mathematical insights like modulo arithmetic. Then I solve them in a simple one-line, brute force approach. Some examples:
What remainder do we get if 1²+2²+3²+4²+…+2018²+2019² is divided by 8? <https://goo.gl/3rdm2m> What is the remainder when 9×99×999×9,999×99,999×999,999×9,999,999×99,999,999×999,999,999 is divided by 2,019? <https://goo.gl/xCUcrH> What is the the LCM and HCF of 2/3, 8/9, 16, 81, and 10/27? <https://www.quora.com/What-is-the-the-LCM-and-HCF-of-2-3-8-9-16-81-and-10-27> What is the average of the sum of the permutations of 6789? <https://goo.gl/YZUuTz> What are the last 3 digits of 1001^1001? <https://goo.gl/1Jrif3> Suppose every term in the sequence 1,2,1,2,2,2,1,2,2,2,2,2,1…… is either 1 or 2. If there are exactly (2k - 1) twos between the k-th one and (k + 1)-the one, find the sum of its first 2016 terms. What is the answer [with method please]? <https://goo.gl/cYbKgs> Skip Skip Cave Cave Consulting LLC On Thu, Jan 17, 2019 at 1:15 PM David Lambert <[email protected]> wrote: > This seems to be an interesting thread so let's talk about Ken Iverson and > notation as a tool for thought. > > In j notation (projecteuler jumps to mind) brute force solutions may be > easily expressed. How many? Prefix the sentence with a tally verb. How > many are there on [0, 10^18) ? Change 100+i.900 to i.1e18 . Problem > solved, on paper. Rethinking the problem as combinations of ways the > digits add to 5 there are 15 solutions. Brutishness can generate test > cases. > > 2 (1: + -&! + -&! + %&! + %&!)~ 3 > 15 > > > Explanation > > 005 1: 500 is it. > > 014 3 -&! 2 permutations of 3 items less the ways in which 0 is 1st > 023 3 -&! 2 > > 113 3 %&! 2 permutations of 3 items divided by the tally of ways to > form the set > 122 3 %&! 2 > > > Relax the constraint, the solution interval is now [0,n), and we want the > solutions tally. > !3 for 014 and 023 > 3%&!2 for 005 113 221 > > 2 ((2 * !@:]) + (3 * %&!)~) 3 > 21 > > side track to implement my brutish solution > NB. "The fluid that passes through is called the filtrate"--wikipedia > NB. mark the filtrate with ones in the logical verb u to adverb Filter > Filter=:(#~`)(`:6) > odometer =: (4 $. $.)@:($&1) > > NB. original problem > (10 #. [: (0 ~: {."1)Filter (5 = +/"1)Filter)@:odometer 6 5 5 > 104 113 122 131 140 203 212 221 230 302 311 320 401 410 500 > > NB. new challenge, solutions tally. 21 agrees with permutations above > brute =: #@:(10 #. (5 = +/"1)Filter)@:odometer > brute 6 6 6 > 21 > > > NB. f is the tally of unique arrangements of items > NB. and I've re-derived a formula taught > NB. in probability & statistics 101 > f =: !@:# % ([: */@:! #/.~) > > brute 6#6 NB. interval [0,1e6) > 252 > > NB. After finally remembering other combinations having digits adding to > 5, > +/ f&> ('ooo'&,L:_1;:'ooF oBE oCD BBD BCC'),'ooBBBC';'oBBBBB' > 252 > > > NB. care to check? The tally of numbers on [0,1e18) having > NB. digits summing to 5 when expressed in base 10 is... 26334 > > >WORDS =: (12#'o')&,L:_1('ooo'&,L:_1 ;:'ooF oBE oCD BBD > BCC'),'ooBBBC';'oBBBBB' > oooooooooooooooooF > ooooooooooooooooBE > ooooooooooooooooCD > oooooooooooooooBBD > oooooooooooooooBCC > ooooooooooooooBBBC > oooooooooooooBBBBB > > +/ f&> WORDS > 26334 > > > > >Date: Wed, 16 Jan 2019 01:12:56 -0600 > >From: "'Skip Cave' via Programming" <[email protected]> > >To: "[email protected]" <[email protected]> > >Subject: [Jprogramming] Implicit Coding > >Message-ID: > > < > caj8lg_cfpxy-s67sp9gdcfwchan5sdzw1sv7fpaha19zmdh...@mail.gmail.com > > > >Content-Type: text/plain; charset="UTF-8" > > > >A Quora question asks "List all the three-digit numbers which have the > >property that the sum of their digits is equal to 5" > > > >I got the answer explicitly, by a straightforward brute force approach:: > >a#~5=+/"1]10#.^:_1 a=:100+i.900 > >104 113 122 131 140 203 212 221 230 302 311 320 401 410 500 > >How can this be written implicitly? > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
