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

Reply via email to